Home > database >  convert text to csv with python pandas
convert text to csv with python pandas

Time:10-19

I want to convert text file to csv file

    import pandas as pd
    readfile = pd.read_csv(r'text.txt')
    readfile.to_csv(r'CSV.csv, index=None)

my text file format:

my text file format

the result:

the result

In the red circle it's add a decimal number follow the data it's duplicate I don't want it to add a decimal number

please suggestion me what to do next, thank you.

and if it possible to read file and convert to csv with limit column please advise!

CodePudding user response:

#just add header = None, since first line of txt is considered header that's why it is managing duplicate column names.
import pandas as pd
readfile = pd.read_csv(r'text.txt',header=None)
readfile.to_csv(r'CSV.csv, index=None)

#sample example output of readfile
        0   1   2   3   4     5   6 7   8
    0   1   2   3   5   0.0 0.0 0.0 4   6
  • Related