我有一个CSV文件,每个字段都用双引号括起来。但是某些字段/字符串本身就含有双引号,我想从该特定字符串中将其移除。
例如- CSV字段中的一个字符串是"My name is "Rajesh" Kumar"。
现在我想将上述字符串替换为"My name is Rajesh Kumar",保留外部双引号。
我尝试了下面的代码,但不幸的是它替换了所有双引号。
file_out = csv.writer(open("file", "w"), doublequote=False, escapechar='\\', delimiter=';',quotechar='"')
with open("file", "r") as f:
content = f.read().replace('"', '')
reader = csv.reader(StringIO(content), doublequote=False, escapechar='\\', delimiter=';'quotechar='"')
for row in reader:
print(row)
file_out.writerow(row)