繁体   English   中英

我的数据在列的值中有逗号,这也是一个分隔符,如何通过 csv.reader 在 python 中读取它

[英]My data has comma in the value of the column which is also a delimiter, how to read it by csv.reader in python

这就是我的数据的样子,

"id","text"
"416752", "i's ** This year’s theme is \"Each for Equal\".**  When: Friday March 6th Time: 10:00am-12:00pm  Where:  HQ 155 Gordon Baker Road, 5th floor, Halifax/Dartmouth Boardoom   Please note there are 2 shifts - one to log your attendance for the event and the other to log your donation, if applicable.    Structure of the Event  10h00am - 10h15am:   Welcome, cupcakes, coffee, social media picture opportunity (#eachforequal)  10h15am – 10h25am:   Intro from WIA, IWD, theme introduction, agenda, community initiative: Dress for Success  10h30am – 11h15am:   Keynote Speaker (Shasta Townsend)  11h30am – 12h00pm:   Panel Stories (Mike Sharun, Dennis Hoffman, Barbara Dahan and Nikki Matarazzo)     **"

以下是我尝试过的逻辑:

import csv
import codecs
file_path=r'C:\data.csv'
f = codecs.open(file_path, encoding = "utf8", errors ='replace')
csvreader = csv.reader(f, delimiter=',')
for row in csvreader:
    print(row)

Output:

['416752', 'i\'s ** This year’s theme is \\Each for Equal\\".**  When: Friday March 6th Time: 10:00am-12:00pm  Where:  HQ 155 Gordon Baker Road', '5th floor', 'Halifax/Dartmouth Boardoom   Please note there are 2 shifts - one to log your attendance for the event and the other to log your donation', 'if applicable.     Structure of the Event    10h00am - 10h15am:   Welcome', 'cupcakes', 'coffee', 'social media picture opportunity (#eachforequal)  10h15am – 10h25am:   Intro from WIA', 'IWD', 'theme introduction', 'agenda', 'community initiative: Dress for Success  10h30am – 11h15am:   Keynote Speaker (Shasta Townsend)  11h30am – 12h00pm:   Panel Stories (Mike Sharun', 'Dennis Hoffman', 'Barbara Dahan and Nikki Matarazzo)     **"']

我得到的不是两个值,而是更多,因为我的数据有很多逗号。

逗号后的空格是问题所在。 使用skipinitialspace=True来解决这个问题。 还建议使用内置的open 使用更新的数据,您还需要 doublequote doublequote=Falseescapechar='\\'

字符串中的双引号默认通过加倍字符进行转义,例如"Say, ""Hi!""" ,但您的示例使用反斜杠进行转义,例如"Say, \"Hi!\"" 这两个附加选项禁用双引号,而是使用转义字符。

import csv

file_path = 'data.csv'
with open(file_path, encoding='utf8', newline='') as f:
    csvreader = csv.reader(f, skipinitialspace=True, doublequote=False, escapechar='\\')
    for row in csvreader:
        print(row)

Output:

['id', 'text']
['416752', 'i\'s ** This year’s theme is "Each for Equal".**  When: Friday March 6th Time: 10:00am-12:00pm  Where:  HQ 155 Gordon Baker Road, 5th floor, Halifax/Dartmouth Boardoom   Please note there are 2 shifts - one to log your attendance for the event and the other to log your donation, if applicable.    Structure of the Event  10h00am - 10h15am:   Welcome, cupcakes, coffee, social media picture opportunity (#eachforequal)  10h15am – 10h25am:   Intro from WIA, IWD, theme introduction, agenda, community initiative: Dress for Success  10h30am – 11h15am:   Keynote Speaker (Shasta Townsend)  11h30am – 12h00pm:   Panel Stories (Mike Sharun, Dennis Hoffman, Barbara Dahan and Nikki Matarazzo)     **']

正如这个问题中提到的为什么 Python CSV 阅读器忽略双引号字段? 您需要添加 skipinitalspace 参数,以便 csv.reader 能够理解引号。

import csv
import codecs
file_path=r'C:\data.csv'
f = codecs.open(file_path, encoding = "utf8", errors ='replace')
csvreader = csv.reader(f, delimiter=',', skipinitialspace=True)
for row in csvreader:
    print(row)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM