Skip to content
Advertisement

txt to csv with python pandas

I have a txt file that I would like to read and export in csv format, but i have one problem.

Data in txt file looks like this |

JavaScript

and I would like to show each columns separately in csv file. Now as you see columns are separated by | . As an example – Sales Organization should be a header and EU01 should be its value and so on.

JavaScript

Advertisement

Answer

Well, Heres what i did:

Let’s call this file, data.txt. I add an additional ‘|’ Before Sales Organisation

Now I strip white space off it:

JavaScript

we then get a clean-looking data.txt:

JavaScript

Now I simply read it into pandas and drop the first column:

JavaScript

Heres the output!

JavaScript

You can now convert it to a CSV, if you like:

df.to_csv('data.csv', index=False)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement