简体   繁体   中英

How to convert a txt file to csv file in cells

I am trying to convert a txt file in the following format:

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

To a csv file, omitting NOTE and in cell format:

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

Example using 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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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