简体   繁体   中英

I need to edit a python script to remove quotes from a csv, then write back to that same csv file, quotes removed

I have seen similar posts to this but they all seem to be print statements (viewing the cleaned data) rather than overwriting the original csv with the cleaned data so I am stuck. When I tried to write back to the csv myself, it just deleted everything in the file. Here is the format of the csv:

30;"unemployed";"married";"primary";"no";1787;"no";"no";"cellular";19;"oct";79;1;-1;0;"unknown";"no"
33;"services";"married";"secondary";"no";4747;"yes";"cellular";11;"may";110;1;339;2;"failure";"no"
35;"management";"single";"tertiary";"no";1470;"yes";"no";"cellular";12;"apr"185;1;330;1;"failure";"no"

It is delimited by semicolons, which is fine, but all text is wrapped in quotes and I only want to remove the quotes and write back to the file. Here is the code I reverted back to that successfully reads the file, removes all quotes, and then prints the results:

import csv
f = open("bank.csv", 'r')
try:
    for row in csv.reader(f, delimiter=';', skipinitialspace=True):
        print(' '.join(row))
finally:
        f.close()

Any help on properly writing back to the csv would be appreciated, thanks!

See here: Python CSV: Remove quotes from value

I've done this basically two different ways, depending on the size of the csv.

  1. You can read the entire csv into a python object (list), do some things and then overwrite the other existing file with the cleaned version
  2. As in the link above, you can use one reader and one writer, Create a new file, and write line by-line as you clean the input from the csv reader, delete the original csv and rename the new one to replace the old file.

In my opinion option #2 is vastly preferable as it avoids the possibility of data loss if your script has an error part way through writing. It also will have lower memory usage.

Finally: It may be possible to open a file as read/write, and iterate line-by-line overwriting as you go: But that will leave you open to half of your file having quotes, and half not if your script crashes part way through.

You could do something like this. Read it in, and write using quoting=csv.QUOTE_NONE

import csv
f = open("bank.csv", 'r')
inputCSV = []
try:
    for row in csv.reader(f, delimiter=';', skipinitialspace=True):
        inputCSV.append(row)
finally:
        f.close()

with open('bank.csv', 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=';')
    for row in inputCSV:
        csvwriter.writerow(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM