我有一个文件,我希望使用CSV阅读器进行解析,它有12行,但有些列包含引号,并且使逗号和单引号和新行更复杂,麻烦的是csv阅读器不处理引号正确地,引号内的引号被视为一个单独的实体,这里是我正在处理的一小部分样本.
ptr = open("myfile")
text = ptr.read()
ptr.close()
for l in csv.reader(text, quotechar='"', delimiter=',',quoting=csv.QUOTE_ALL, skipinitialspace=True):
print l
Run Code Online (Sandbox Code Playgroud)
该文件包含:
"0","11/21/2013","NEWYORK","USA
Atlantic ","the person replied \"this quote\" to which i was shocked,
this came as an utter surprise"
"1","10/18/2013","London","UK","please note the message \"next quote\"
is invalid"
"2","08/11/2014","Paris","France",
"the region is in a very important geo strategic importance"
Run Code Online (Sandbox Code Playgroud)
你必须在读者中设置escapechar:
csv.reader(..., escapechar='\\')
Run Code Online (Sandbox Code Playgroud)
默认情况下None(不知道为什么).
第二件事是您错误地初始化了阅读器.您不会将字符串传递给阅读器,而是传递给流:
with open("myfile") as fo:
reader = csv.reader(
fo,
quotechar='"',
delimiter=',',
quoting=csv.QUOTE_ALL,
skipinitialspace=True,
escapechar='\\'
)
for row in reader:
print row
Run Code Online (Sandbox Code Playgroud)