简体   繁体   English

我需要编辑 python 脚本以从 csv 中删除引号,然后写回同一个 csv 文件,引号已删除

[英]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.我看过与此类似的帖子,但它们似乎都是打印语句(查看清理后的数据),而不是用清理后的数据覆盖原始 csv,所以我被卡住了。 When I tried to write back to the csv myself, it just deleted everything in the file.当我尝试自己写回 csv 时,它只是删除了文件中的所有内容。 Here is the format of the csv: 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!任何有关正确写回 csv 的帮助将不胜感激,谢谢!

See here: Python CSV: Remove quotes from value请参阅此处: Python CSV:从值中删除引号

I've done this basically two different ways, depending on the size of the csv.根据 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您可以将整个 csv 读入 python object (列表),做一些事情,然后用清理后的版本覆盖其他现有文件
  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.如上面的链接,您可以使用一个读取器和一个写入器,创建一个新文件,并在清除 csv 读取器的输入时逐行写入,删除原始 csv 并重命名新文件以替换旧文件.

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.在我看来,选项#2 是非常可取的,因为如果您的脚本在编写过程中出现错误,它可以避免数据丢失的可能性。 It also will have lower memory usage.它还将具有较低的 memory 使用率。

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.最后:可以以读/写方式打开文件,并像您一样逐行迭代 go:但这将使您打开一半的文件有引号,如果您的脚本中途崩溃,则一半不会通过。

You could do something like this.你可以做这样的事情。 Read it in, and write using quoting=csv.QUOTE_NONE读入并使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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