我有一个由Python脚本生成的巨大CSV。一些单元格包含数据数组,而其他单元格包含单个项数组。一些例子:
cell01 == ['"July, 2002"', 'CUREE Publication No. CEA-01.', 'Project No. 3126', 'Prepared for Consortium of Universities for Research in Earthquake Engineering.']
cell02 == ['[Memorandum from Ralph J. Johnson on Andy Place].']
cell03 == ["Financial statements for the years ended March 31, 1991 and 1990 and independent auditors' report"]
理想情况下,我希望将所有这些数据解析为如下结构:
cell01_parsed[0] == '"July, 2002"'
cell01_parsed[1] == 'CUREE Publication No. CEA-01.'
cell01_parsed[2] == 'Project No. 3126'
cell01_parsed[3] == 'Prepared for Consortium of Universities for Research in Earthquake Engineering.'
cell02_parsed == '[Memorandum from Ralph J. Johnson on Andy Place].'
cell03_parsed == 'Financial statements for the years ended March 31, 1991 and 1990 and independent auditors\' report'
但是,当我使用csv.reader()
或csv.DictReader()
时,这些行被解析为字符串,而不是数组。有什么简单的方法可以做到这一点?我不能使用split(',')
,因为有些字符串的项中间有逗号。你知道吗
您可以尝试用regex(找到一个适合您的数据)来拆分字符串,如下所示: 你知道吗
相关问题 更多 >
编程相关推荐