1

Keep getting this split error, when trying to split up my list Word by Word, line by line.

I got a file which contains links, +20000 links. These links is in a list called "links"

my code so far:

import networkx as nx

# Create graph
network_graph = nx.Graph()

path = []
with open('paths_finished.tsv','r') as tsv:
    paths = [line.strip().split('\t') for line in tsv]  
    newPath = paths[16:]


links = []    
for line in newPath:
    links.append(line[3:4])

newList = []

for i in links:
    newList.append(i.split(';'))

print newList

The lenght of the links list = 51318. I want to split up the " ; " in every links in my list.

For example the first link in the file are:

['14th_century;15th_century;16th_century;Pacific_Ocean;Atlantic_Ocean;Accra;Africa;Atlantic_slave_trade;African_slave_trade'], 

Then I want to split it up Word by Word, so I got:

['14th_century 15th_century 16th_century Pacific_Ocean Atlantic_Ocean Accra Africa Atlantic_slave_trade African_slave_trade'], 
6
  • 1
    I suspect your indentation for the for line in newPath loop is either off, or entirely wrong (you'll only process the last newPath). Commented Apr 7, 2014 at 10:33
  • So you have a list that contains one element (a very long string) and you want to return a list with the very same string element where ; is replaced by ' ' (whitespace)? Or you want a list where each word is an element? Commented Apr 7, 2014 at 11:07
  • yes, thats what i want ..:) Commented Apr 7, 2014 at 11:09
  • l is your first list. x = [l[0].replace(';', ' ')] (list with one string) | y = l[0].split(';') (list of words) Commented Apr 7, 2014 at 11:11
  • Why are you slicing? links.append(line[3:4]) should just be links.append(line[3]). Commented Apr 7, 2014 at 11:14

1 Answer 1

2

First thing - as Martijn Pieters said, your indentation is off. Its hard to guess exactly what you mean, please fix it. But:

paths = [line.strip().split('\t') for line in tsv]  

line.split('\t') already returns a list. You put that list into path so path is a list of lists. You iterate over that list of lists here:

for line in newPath:
   links.append(line[3:4])

so links will also be a list of lists. And finally:

for i in links:
   newList.append(i.split(';'))

you try to call split for i - which is a list. split is a member function of str and does not exist for lists - hence your error.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm new to python, so i'm really sorry if i done something wrong :P So you saying its stupid to use that many lists?
Hey, Im not saying its stupid. I was just explaining your code. You probably could have just used split() with no delimiters on the whole .tsv file. It would split on \t and \n so you have one list of strings which im guessing is what you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.