0

I have a txt file which contains data as in below format

"column1,column2,column3,column4,column5,column6,column7,column8"
"abc,abc,abc,10,datetime,abc,abc,abc"
"xyz,xyz,""xyz1,xyz2"",2,datetime2,xyz,xyz,xyz"
"xyz,xyz,""xyz1 , xyz2"",2,datetime2,xyz,xyz,xyz"

I want to convert it into a Pandas DataFrame which will be having 8 columns of header same as row 1

it is different from normal/regular Dataframe question. I tried with following code,

df = pd.read_csv('tst.txt')

But output was

    column1,column2,column3,column4,column5,column6,column7,column8
0   abc,abc,abc,10,datetime,abc,abc,abc
1   xyz,xyz,"xyz1,xyz2",2,datetime2,xyz,xyz,xyz
2   xyz,xyz,"xyz1 , xyz2",2,datetime2,xyz,xyz,xyz

I tried with other things as well like

df1 = pd.DataFrame([line.replace(' , ','$$$').replace('"','').replace('\n','').split(',') for line in open('tst.txt')])

but output was different and not expected

    0   1   2   3   4   5   6   7   8
0   column1     column2     column3     column4     column5     column6     column7     column8     None
1   abc     abc     abc     10  datetime    abc     abc     abc     None
2   xyz     xyz     xyz1    xyz2    2   datetime2   xyz     xyz     xyz
3   xyz     xyz     xyz1$$$xyz2     2   datetime2   xyz     xyz     xyz     None

So you can see here that only 8 columns should be there not 9. datetime should be in 5th column.

Actual output should be like,

    column1     column2     column3     column4     column5     column6     column7     column8
0   abc     abc     abc     10  datetime    abc     abc     abc
1   xyz     xyz     xyz1,xyz2   2   datetime2   xyz     xyz     xyz
2   xyz     xyz     xyz1 , xyz2     2   datetime2   xyz     xyz     xyz
2

1 Answer 1

1

Try pass the quotechar with "

df=pd.read_csv('tst.txt', quotechar='"', sep=',')

  column1 column2      column3  column4    column5 column6 column7 column8
0     abc     abc          abc       10   datetime     abc     abc     abc
1     xyz     xyz    xyz1,xyz2        2  datetime2     xyz     xyz     xyz
2     xyz     xyz  xyz1 , xyz2        2  datetime2     xyz     xyz     xyz
Sign up to request clarification or add additional context in comments.

1 Comment

I am still not getting this kind of result. I am doing same as above still not getting desired one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.