Python从csv中删除每个双引号
问题描述:
我有一个如下所示的csv文件.
Hi I have a csv file that looks like the following.
"AB" ; "AA" ; "BA" ; "HI"
"CD" ; "BB" ; "BC" ; "JK"
"EF" ; "CC" ; "CE" ; "LM"
"GH" ; "DD" ; "DG" ; "MN"
如何获取以下代码以从csv文件的每一列中去除所有双引号,因为现在它仅去除第一列.谢谢
How can I get the following code to strip off all the double quotes from every column in a csv file as for now it strips only the first column. Thanks
import csv
f = open("wakhawakha.csv", 'rt')
try:
for row in csv.reader(f, delimiter=' ', skipinitialspace=True):
print('|'.join(row))
finally:
f.close()
答
将其打开并首先读取字符串.
Open it and read the string first.
import csv
with open("wakhawakha.csv", 'rt') as f:
data = f.read()
new_data = data.replace('"', '')
for row in csv.reader(new_data.splitlines(), delimiter=' ', skipinitialspace=True):
print ('|'.join(row))