【发布时间】:2017-08-02 04:26:31
【问题描述】:
我是 python 新手,正在尝试使用正则表达式或 CSV 阅读器解决以下问题。
我的输入字符串格式如下:
"some text"|"sample\" name|place\\""|"some other text\\""
预期输出是:
'some text','sample" name|place\"','some other text\"'
我的字符串有分隔符、转义字符和引号字符。当我将输入文本保存在文件中并使用 csv 阅读器读取时,它按预期工作。
with open('inputfile.csv') as csvfile:
inputValue = csv.reader(csvfile, delimiter='|', quotechar='"',escapechar = '\\')
for eachVal in inputValue:
print(','.join(eachVal))
但是当我将输入值放入列表并使用 CSV 阅读器时,它没有给出正确的输出。
inputText = '"some text"|"sample\" name|place\\""|"some other text\\""'
inputValue = csv.reader(inputText, delimiter='|',quotechar='"', escapechar = '\\')
for eachVal in inputValue:
print(','.join(eachVal))
对这个 CSV 阅读器的任何帮助或任何使用正则表达式的解决方案都会很棒。谢谢。
【问题讨论】:
标签: python regex csv backslash reader