使用 python 或 R 替换 csv 中的特定特殊字符组合 \"
Replacing specific special-character combination \" in csv using python or R
我有一大批包含数字和字符串列的CSV,其中偶尔会出现两个特殊字符的组合\"
。这种组合偶尔会出现在字符串的末尾,例如 "string\""
,并且相邻的双引号可能会在尝试导入时混淆某些软件。我想去除 \"
的所有 CSV(但显然不是单个 "
或 \
字符),然后去除 save/replace CSV。我该怎么做? Python 或 R 首选。
使用输入文件
slash_quote.csv:
"a","string\"","b"
使用代码:
files = ("slash_quote.csv",)
for filename in files:
with open(filename, "r") as f:
with open(filename+"_new", "w") as outfile:
outfile.write(f.read().replace(r'\"', ''))
# if replacement is desired include
os.rename(f"{filename}_new", filename)
输出文件:
"a","string","b"
我有一大批包含数字和字符串列的CSV,其中偶尔会出现两个特殊字符的组合\"
。这种组合偶尔会出现在字符串的末尾,例如 "string\""
,并且相邻的双引号可能会在尝试导入时混淆某些软件。我想去除 \"
的所有 CSV(但显然不是单个 "
或 \
字符),然后去除 save/replace CSV。我该怎么做? Python 或 R 首选。
使用输入文件
slash_quote.csv:
"a","string\"","b"
使用代码:
files = ("slash_quote.csv",)
for filename in files:
with open(filename, "r") as f:
with open(filename+"_new", "w") as outfile:
outfile.write(f.read().replace(r'\"', ''))
# if replacement is desired include
os.rename(f"{filename}_new", filename)
输出文件:
"a","string","b"