Skip to content
Advertisement

How to save pandas textmanipulation as csv in the correct form

I have a *.txt file with numbers. I want to eliminate the spaces. The raw data looks like this

JavaScript

If I use the following

JavaScript

I get

JavaScript

which I want. But if I save it to csv with

JavaScript

I get as file values:

JavaScript

If I use the quoating = csv.None or 3 (same)

JavaScript

The file looks like:

JavaScript

Just using space or nothing as escapechar does not work.

If I just remove the spaces without adding quotes or commas and then save via:

JavaScript

I get:

JavaScript

missing the comma.

Adding only the comma and saving as above gets me

JavaScript

wrong place :-)

As you can see, I am getting mad over missing my target by inches, while it is most likely super easy. I probably will switch to regex :-)

Advertisement

Answer

The output is expected. As you manually added ' to the strings, they are retained in the output. There’s no need to manually add quotation marks at all, just set the correct options for to_csv:

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