代码之家  ›  专栏  ›  技术社区  ›  CsharpNoob

如何读取包含数据块的复杂txt文件,并将其保存为python中的csv文件?

  •  0
  • CsharpNoob  · 技术社区  · 9 年前

    如果我有一个这样组织的文件

    ++++++++++++++
    Country 1
    
    **this sentence is not important.
    **date 25.09.2017, also not important
    *******
    Address
    **Office
    
            Address A, 100 City. Country X
    **work time 09h00-16h00<br>9h00-14h00
    **www.example.com
    **emal@example.com;
    **012/345 67 89
    **téléfax 123/456 67 89
    *******
    Address
    **Home Office
    
            Address A, 200 City. Country X
    **email2@example.com;
    **001/000 00 00
    **téléfax 111/111 11 11
    *******
    Address
    **Living address
    
            Address 0, 123 City
    **info@example.ch
    **000/000 00 00
    **téléfax 222/222 22 22
    ++++++++++++++
    Country 2
    
    **this sentence is not important.
    **date 25.09.2017, also not important
    *******
    Address
    **Office
    
            AAA 11, 30 City 
    
            BBB 22, 30 City
    **work time 08h00-12h30  
    **www.example.com
    **info@example.com
    **000/000 00 00
    **téléfax 111/11 11 11
    *******
    
    ETC
    

    我想把数据放在csv文件中,包含以下列:

    Country (Line right after ++++++++++++++), Address (Line right after *******), Office (after **), WorkTime (after **), Website (after **), Email (after **), Phone (after **), Fax (after **)
    

    在Python中如何实现?问题是,在一些列表中缺少数据,所以我知道csv文件中的一些行最终会变得一团糟,但我不介意在完成此操作后手动调整数据库。另一个问题是,国家名称不同,所以我需要使用++++作为分隔符。

    我试过这样的东西

    import csv
    with open('listofdata.txt', 'r') as FILE:
       DATA = FILE.read()
    
    LIST = DATA.split('++++++++++++++')
    
    LIST2 = []
    LIST3 = []
    LIST4 = []
    
    for ITEMS in LIST:
        LIST2 = ITEMS.split('*******')    
        for items2 in LIST2:
            LIST3 = items2.split('**')
            LIST4.append(LIST3)
    
    
    with open('file.csv', 'w') as CSV:
        for ITEMS in LIST4:
            csv.write(ITEMS)
    

    错误:`回溯(上次最近的调用): 文件“test.py”,第22行,in csv。写入(项) AttributeError:“module”对象没有属性“write”

    `

    2 回复  |  直到 9 年前
        1
  •  1
  •   Ali    9 年前

    在最后一行中,您编写了文件对象“csv”而不是“csv”,这就是出现错误的原因。

    我在代码中添加了如何在python中使用csv模块的过程。

    代码:

    import csv
    with open('listofdata.txt', 'r') as FILE:
       DATA = FILE.read()
    
    LIST = DATA.split('++++++++++++++')
    
    LIST2 = []
    LIST3 = []
    LIST4 = []
    
    for ITEMS in LIST:
        LIST2 = ITEMS.split('*******')
        for items2 in LIST2:
            LIST3 = items2.split('**')
            LIST4.append(LIST3)
    
    with open('file.csv', 'w') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',')
        for ITEMS in LIST4:
            spamwriter.writerow(ITEMS)
    

    输出:

    ""
    
    "
    Country 1
    
    ","this sentence is not important.
    ","date 25.09.2017, also not important
    "
    
    "
    Address
    ","Office
    
            Address A, 100 City. Country X
    ","work time 09h00-16h00<br>9h00-14h00
    ","www.example.com
    ","emal@example.com;
    ","012/345 67 89
    ","téléfax 123/456 67 89
    "
    
    "
    Address
    ","Home Office
    
            Address A, 200 City. Country X
    ","email2@example.com;
    ","001/000 00 00
    ","téléfax 111/111 11 11
    "
    
    "
    Address
    ","Living address
    
            Address 0, 123 City
    ","info@example.ch
    ","000/000 00 00
    ","téléfax 222/222 22 22
    "
    
    "
    Country 2
    
    ","this sentence is not important.
    ","date 25.09.2017, also not important
    "
    
    "
    Address
    ","Office
    
            AAA 11, 30 City
    
            BBB 22, 30 City
    ","work time 08h00-12h30
    ","www.example.com
    ","info@example.com
    ","000/000 00 00
    ","téléfax 111/11 11 11
    "
    
    "
    "
    
        2
  •  0
  •   akonieczny    9 年前

    csv.writer . 但首先,您必须为语法分析器的结构做好准备 listofdata.txt 文件,然后您可以将数据保存到csv文件。

    或者,您可以使用 csv.DictWriter 但无论如何,您必须首先准备解析器。