Home > database >  How to convert text file with data that has missing placerholders into CSV file?
How to convert text file with data that has missing placerholders into CSV file?

Time:08-15

I am trying to convert text file into CSV file, but text file data contains missing placeholders such as below

  • "-" is divider
apple - red - sweet - fruit - apple pie    
carrot - orange - - vegetable - carrot cake     
eggplant - purple - watery- vegetable -        
import cvs
with open('data.txt', 'r') as file:
    stripped = (line.strip() for line in file)
    lines = (line.split("-") for line in stripped if line)
    with open('text.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('name', 'color', 'taste', 'category', 'recipe'))
        writer.writerows(lines)

What is the easiest way to deal with missing placeholders?

CodePudding user response:

You could try:

import csv

with open("data.txt", "r") as fin, open("data.csv", "w") as fout:
    writer = csv.writer(fout)
    writer.writerow(('name', 'color', 'taste', 'category', 'recipe'))
    writer.writerows(
        [item.strip() for item in row] for row in csv.reader(fin, delimiter="-")    
    )

This is essentially swapping out - for , and stripping away whitespace.

Result for input file data.txt

apple - red - sweet - fruit - apple pie    
carrot - orange - - vegetable - carrot cake     
eggplant - purple - watery- vegetable - 

is the file data.csv with the content

name,color,taste,category,recipe
apple,red,sweet,fruit,apple pie
carrot,orange,,vegetable,carrot cake
eggplant,purple,watery,vegetable,

CodePudding user response:

You can try this:

allfood = []
s = '''apple - red - sweet - fruit - apple pie \
      carrot - orange - - vegetable - carrot cake     \
      eggplant - purple - watery- vegetable     '''
lines = s.split('\n')
for line in lines:
    if line.count('-') == 3:
        line  = '-'
    allfood.append([x.strip() for x in line.split('-')])
print(allfood)

Thanks.

  • Related