不断收到错误“列表"对象没有属性“拆分" [英] Keep getting error 'list' object has no attribute 'split'

查看:35
本文介绍了不断收到错误“列表"对象没有属性“拆分"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试逐字逐行拆分我的列表时,不断收到此拆分错误.

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

我得到了一个包含链接的文件,+20000 个链接.这些链接位于名为链接"的列表中

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

到目前为止我的代码:

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

链接列表的长度 = 51318.我想拆分列表中每个链接中的;".

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

推荐答案

第一件事 - 正如 Martijn Pieters 所说,缩进已关闭.很难准确猜测您的意思,请修复它.但是:

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') 已经返回一个列表.您将该列表放入 path 中,所以 path 是一个列表列表.您在这里迭代该列表:

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(';'))

您尝试为 i 调用 split - 这是一个列表.split 是 str 的成员函数,不存在于列表中 - 因此您的错误.

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.

这篇关于不断收到错误“列表"对象没有属性“拆分"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆