0

I have this tsv file and i want to remove the ; from the list but it keeps giving me an error message 'AttributeError: 'list' object has no attribute '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

output:

   ['14th_century;15th_century;16th_century]

How do i split the ';' in this output?

2

3 Answers 3

1

row is a list, so the slice row[3:4] is also a list.

You should just use row[3] to get the (string) item from the list

for row in ar:
      h.append(row[3].split(';'))

You may also use a list comprehension

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
Sign up to request clarification or add additional context in comments.

2 Comments

It says indexError list out of range
@user3322871, do you have an empty line? You can add a condition if len(row) > 3 to prevent that
0

You can do:

>>> my_output = ['14th_century;15th_century;16th_century']   #this is your current output

>>> print my_output[0].split(';')
['14th_century', '15th_century', '16th_century']

This takes the first element of your output above and then prints the list based on the string split on ';'

2 Comments

Is it me or questions related to this one string list keep popping up pretty often?
@HerrActress, Huh, yeah! I think we met a few questions ago. Well, nice to see you again!
0

try this ,

>>> ''.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']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.