Home > Net >  Edit CSV file with Python
Edit CSV file with Python

Time:12-30

I have a CSV that looks like this

"String1","variablelengthstring1","No"
"String2","variablelengthstring2","No"

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

String1 variablelengthstring1
String2 variablelengthstring2

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

CodePudding user response:

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:

with open('test.csv') as csv:
    for row in csv.readlines():
        cols = [s.replace('"', '') for s in row.split(',')]
        print(cols[0], cols[1])

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

python program.py > output.txt

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

lines = []
with open('test.csv') as csv:
    for row in csv.readlines():
        cols = [s.replace('"', '') for s in row.split(',')]
        lines.append(f'{cols[0]} {cols[1]}\n')

with open('out.txt', 'w') as out:
    out.writelines(lines)
  • Related