Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Edit CSV file with Python

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 ,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading