【问题标题】:Replacing specific special-character combination \" in csv using python or R使用python或R替换csv中的特定特殊字符组合\"
【发布时间】:2021-10-14 22:04:12
【问题描述】:

我有大量包含数字和字符串列的 CSV,其中偶尔会出现两个特殊字符 \" 的组合。这种组合偶尔会出现在字符串的末尾,例如"string\"",并且相邻的双引号会在尝试导入时混淆某些软件。我想剥离\" 的所有CSV(但显然不是单个"\ 字符),然后保存/替换CSV。我该怎么做?首选 Python 或 R。

【问题讨论】:

标签: python r csv replace special-characters


【解决方案1】:

使用输入文件

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"

【讨论】:

    猜你喜欢
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多