简体   繁体   English

如何在单元格中将txt文件转换为csv文件

[英]How to convert a txt file to csv file in cells

I am trying to convert a txt file in the following format: 我正在尝试将txt文件转换为以下格式:

NOTE
Date : 2/12/2019
Name : SomeName
Amount : 9000.0
Total : 59000.0
3ABA2363F5305877265757265784B2EB94ABxxxxxxxxxxxxxxxx

To a csv file, omitting NOTE and in cell format: 对于csv文件,省略NOTE并采用单元格格式:

Date,Name,Amount,Total,Signature
2/12/2019, SomeName,9000,59000,3ABA2363F5305877265757265784B2EB94ABxxxxxxxxxxxxxxxx

What i have archived to do so far is: 到目前为止,我存档要做的是:

Date,Number,Amount,Total,Signature
['NOTE'],"['Date', '2/12/2019']","['Name', 'SomeName']","['Amount', '9000.0']","['Total', '59000.0']",['3ABA2363F5305877265757265784B2EB94ABxxxxxxxxxxxxxxxx']

Here is my code: 这是我的代码:

with open('example.txt', 'r') as in_file:
    stripped = [line.replace(":","").split() for line in in_file]
    zipped = zip([stripped]*1)
    with open('out_file.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('Date', 'Number', 'Amount', 'Total', 'Signature'))
        for group in zipped:
            writer.writerows(group)

Question : to convert a txt file to csv file in cells 问题 :将txt文件转换为单元格中的csv文件

Example using csv.DictWriter . 使用csv.DictWriter示例。

import io, csv

TXT = """NOTE
Date : 2/12/2019
Name : SomeName
Amount : 9000.0
Total : 59000.0
3ABA2363F5305877265757265784B2EB94ABxxxxxxxxxxxxxxxx
NOTE
Date : 2/12/2019
Name : SomeName
Amount : 9000.0
Total : 59000.0
3ABA2363F5305877265757265784B2EB94ABxxxxxxxxxxxxxxxx
"""

with io.StringIO(TXT) as in_file,\
    io.StringIO() as out_file:

    fieldnames = ['Date', 'Name', 'Amount', 'Total', 'Signature']
    writer = csv.DictWriter(out_file, fieldnames=fieldnames)
    writer.writeheader()

    while True:
        data = {}
        for row in range(6):
            try:
                line = next(in_file)
                s = line.rstrip().split(':')

                if len(s) == 2:
                    data[s[0].strip()] = s[1].strip()
                else:
                    data['Signature'] = s[0]

            except StopIteration:
                break

        if data:
            writer.writerow(data)
        else:
            break

    print('{}'.format(out_file.getvalue()))

Output : 输出

 Date,Name,Amount,Total,Signature 2/12/2019,SomeName,9000.0,59000.0,3ABA2363F5305877265757265784B2EB94ABxxxxxxxxxxxxxxxx 2/12/2019,SomeName,9000.0,59000.0,3ABA2363F5305877265757265784B2EB94ABxxxxxxxxxxxxxxxx 

How does the format change? 格式如何更改? (Like what are possible changes). (就像可能发生的变化一样)。


import csv
with open('inp', 'r') as in_file:
    dic = dict()
    a = []
    for line in in_file:
        a.append(line.replace(":","").split())

    a.pop(0) #NOTE
    last = a.pop().pop() #no key
    for i in a:
        dic[i[0]] = i[1]
    with open('out_file.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(dic.keys())
        toWrite = list(dic.values())
        toWrite.append(last)
        writer.writerow(toWrite)

To add "signature" into the first line you need to change dic.keys() into list and append a string to it (like in case of values with the last item) 要将“签名”添加到第一行,您需要将dic.keys()更改为list并在dic.keys()附加一个字符串(例如,如果值带有最后一项)

Or like this: 或像这样:


import csv
with open('inp', 'r') as in_file:
    a = []
    for line in in_file:
        a.append(line.replace(":","").split())

    a.pop(0) #NOTE
    with open('out_file.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('Date', 'Number', 'Amount', 'Total', 'Signature'))
        toWrite = []
        for i in a:
            toWrite.append(i.pop())
        writer.writerow(toWrite)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM