Skip to content
Advertisement

Edit CSV file with Python

I have a CSV that looks like this

JavaScript

String1 and string2 does NOT contain the characters " or ,

variablelengthstring1 and variablelengthstring2 does NOT contain the characters " or ,

The output file should look like this

JavaScript

So I want to remove the last 6 chars on each line, i.e.
","No"

Then, I want to remove all " from the file

Then, I want to replace all , with a space

That should safely accomplish what I am trying to do

Advertisement

Answer

The easiest way is to just create a program that reads the file line by line, splitting each line up with .split(','), removing all "s from the string, then printing the first parts. Like this:

JavaScript

If you need to output it to a file, then you can use redirection in your command line. Just run the program like this:

JavaScript

Alternatively, you can use another nested with statement and .writelines(), like this:

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