简体   繁体   中英

Keep getting error 'list' object has no attribute 'split'

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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