[英]How do I replace double quotes inside double quotes with empty character in CSV fields?
I have a CSV file with each fields quoted in double quotes.我有一个 CSV 文件,每个字段都用双引号引用。 But some of the fields/strings itself has double quotes inside it and I want to remove them from that particular string.
但是某些字段/字符串本身内部有双引号,我想从该特定字符串中删除它们。
For example- One of the string in CSV fields is "My name is "Rajesh" Kumar".例如 - CSV 字段中的字符串之一是“我的名字是“Rajesh”Kumar”。
Now I want to replace the above string as "My name is Rajesh Kumar", restoring the double quotes outside.现在我想将上面的字符串替换为“我的名字是Rajesh Kumar”,恢复外面的双引号。
I tried the below code, but unfortunately it replaces all the double quotes.我尝试了下面的代码,但不幸的是它替换了所有的双引号。
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)
You could replace all quotes in a substring that does not include the first and last characters of your original string.您可以替换不包含原始字符串的第一个和最后一个字符的子字符串中的所有引号。
row = "\"My name is \"Rajesh\" Kumar\""
print(row)
row = row[0] + row[1:-1].replace('\"', '') + row[-1]
print(row)
or或者
row = "\"My name is \"Rajesh\" Kumar\""
print(row)
row = "\"{}\"".format(row[1:-1].replace('\"', ''))
print(row)
Output:输出:
"My name is "Rajesh" Kumar"
"My name is Rajesh Kumar"
Found another post that basically covers what is being asked: Regular expression replace except first and last characters找到了另一篇基本上涵盖了所问内容的帖子: 正则表达式替换除了第一个和最后一个字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.