当我使用以下代码
阅读CSV标题时def read_header(gram):
CSV_tag_read = csv.reader(open("Dataset/MALE_Training/"+str(gram)+"/"+str(gram)+".csv","rb"),delimiter='"')
header=CSV_tag_read.next()
标题(csv文件的第一行)如下
Sample1"('CC', 'CD')"('CD', 'DT')"('DT', 'EX')"('EX', 'IN')"('IN', 'JJ')"('JJ', 'JJR')"('JJR', 'JJS')"('JJS', 'MD')"('MD', 'NN')"('NN', 'NNS')"('NNS', 'PRP')"('PRP', 'RB')"('RB', 'RBR')"('RBR', 'RP')"('RP', 'TO')"('TO', 'VB')"('VB', 'VBD')"('VBD', 'VBG')"('VBG', 'VBN')"('VBN', 'VBP')"('VBP', 'VBZ')"('VBZ', 'WDT')"('WDT', 'WP')"('WP', 'WRB')
我希望我的输出列表包含
['Sample1', '(CC, CD)', '(CD, DT)', '(DT, EX)', '(EX, IN)', '(IN, JJ)', '(JJ, JJR)', '(JJR, JJS)', '(JJS, MD)', '(MD, NN)', '(NN, NNS)', '(NNS, PRP)', '(PRP, RB)', '(RB, RBR)', '(RBR, RP)', '(RP, TO)', '(TO, VB)', '(VB, VBD)', '(VBD, VBG)', '(VBG, VBN)', '(VBN, VBP)', '(VBP, VBZ)', '(VBZ, WDT)', '(WDT, WP)', '(WP, WRB)']
答案 0 :(得分:6)
>>> import ast
>>> a = "('TO', 'RP', 'TO')"
>>> b = ast.literal_eval(a)
>>> b
('TO', 'RP', 'TO')
>>> b[0]
'TO'
或者
>>> [x.strip("' ") for x in a.strip('()').split(',')]
['TO', 'RP', 'TO']
与更新的问题对应的更新的答案:
with open('/path/to/csv-file.csv') as f:
reader = csv.reader(f, delimiter='"')
header = [column.replace("'", "") for column in header]