Home > Software engineering >  text to csv conversion using python?
text to csv conversion using python?

Time:03-11

I have txt file which look like this:

Quod equidem non reprehendo;
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quibus natura iure responderit non esse verum aliunde finem beate vivendi, a se principia rei gerendae peti; Quae enim adhuc protulisti, popularia sunt, ego autem a te elegantiora desidero. Duo Reges: constructio interrete. Tum Lucius: Mihi vero ista valde probata sunt, quod item fratri puto. Bestiarum vero nullum iudicium puto. Nihil enim iam habes, quod ad corpus referas; Deinde prima illa, quae in congressu solemus: Quid tu, inquit, huc? Et homini, qui ceteris animantibus plurimum praestat, praecipue a natura nihil datum esse dicemus?
=========================================================================
Planet   Number   festival   animal
                  colour     book
Mercury  First    firecrack  phone
Venus    Last     kite       computer
Earth    Country  rangoli    tv
Jupiter  C.COD     bomb       

---------------------------------------------------------------------
11      4526      diwali      dog
                  holi        bigb
12      Joe       diwali      111
45      Doe       sankaranti  acer
65      UK        diwali      pan
67      22        diwali      
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Planet   Number   festival   animal
                  colour     book
Mercury  First    firecrack  phone
Venus    Last     kite       computer
Earth    Country  rangoli    tv
Jupiter  C.COD     bomb     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
45       5637      ganesh    tiger
                   holi      cinema
67       micael    holi      222
78       john      diwali    xamoi
90       france    diwali    hp
34       34        diwali   

I want to convert this text file into csv format. The output I would like to show: output:output

My code:

from itertools import groupby, chain
 with open("file.txt", "r") as fin,\
      open("file.csv", "w") as fout:
for key, group in groupby(fin, key=lambda line: bool(line.strip())):
    if key:
        zipped = zip(*(line.rstrip().split() for line in group))
        fout.write(",".join(chain(*zipped))   "\n")

CodePudding user response:

This will do what you ask. It's just a matter of gathering up fields until we get a trigger to write them, AND ignoring the beginning text, AND ignoring all the headers except the first.

fin = open('file.txt')
fout = open('file.csv','w')

gather = []
skipping = True
first = True
for line in fin:
    if skipping:
        skipping = line.find('====') < 0
    elif line.find('----') >= 0:
        if gather and (first or gather[0] != 'Planet'):
            print( ','.join(gather), file=fout )
        gather = []
        first = False
    else:
        gather.extend( line.strip().split() )

if gather:
    print( ','.join(gather), file=fout )

CodePudding user response:

I believe you can use Pandas lib to convert a txt file to csv

# importing panda library
import pandas as pd
  
# readinag given csv file
# and creating dataframe
dataframe1 = pd.read_csv("input_file.txt")
  
# storing this dataframe in a csv file
dataframe1.to_csv('output_file.csv', 
                  index = None)
  • Related