我有这个tsv文件,我想删除;从列表中,它不断给我一个错误消息' AttributeError:' list'对象没有属性' split''
import csv
h = []
with open('paths_finished.tsv', 'rb') as csvfile:
ar = csv.reader(csvfile, dialect='excel-tab')
for row in ar:
h.append(row[3:4].split(';'))
print h
输出:
['14th_century;15th_century;16th_century]
我如何拆分&#39 ;;'在这个输出?
答案 0 :(得分:1)
row
是一个列表,因此切片row[3:4]
也是一个列表。
您应该使用row[3]
从列表中获取(字符串)项
for row in ar:
h.append(row[3].split(';'))
您也可以使用列表理解
import csv
with open('paths_finished.tsv', 'rb') as csvfile:
ar = csv.reader(csvfile, dialect='excel-tab')
h = [row[3].split(';') for row in ar if len(row) > 3]
print h
答案 1 :(得分:0)
你可以这样做:
>>> my_output = ['14th_century;15th_century;16th_century'] #this is your current output
>>> print my_output[0].split(';')
['14th_century', '15th_century', '16th_century']
这将获取上面输出的第一个元素,然后根据';'
上的字符串拆分打印列表
答案 2 :(得分:0)
试试这个,
>>> ''.join(a)
'14th_century;15th_century;16th_century'
>>> a=''.join(a)
>>> a
'14th_century;15th_century;16th_century'
>>> a.split(';')
['14th_century', '15th_century', '16th_century']