[英]How to get rid of empty strings in Python when splitting a list?
我有一个输入文件,其中包含以下行:
['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n', # and so on....]
我用readlines
格式化了它,对此:
['Some Name', '', '', '', '2.0 2.0 1.3\n']
['Another Name', '', '', '', '1.0 9.0 1.0\n']
['Another Name', '', '', '', '1.0 9.0 1.0\n']
# and so on
我想做的是将名字放在彼此之下,而我正在摆脱_符号。
这是我的代码:
def openFile():
fileFolder = open('TEXTFILE', 'r')
readMyFile = fileFolder.readlines()
for line in readFile:
line = line.split("_")
personNames = line[0]
print personNames
print openFile()
所以我现在得到的是:
Some Name
Another Name
Another Name
这很酷,但我想更进一步,这就是我陷入困境的地方。 我现在要做的是摆脱空字符串( ""
)并打印你可以看到的数字,就在我已经格式化的名字旁边 。
我以为我可以这样做:
for line in readFile:
line = line.split("_")
get_rid_of_spaces = line.split() #getting rid of spaces too
personNames = line[0]
但这给了我这个错误:
AttributeError: 'list' object has no attribute 'split'
我怎样才能做到这一点? 我想学习这个。
我也试过递增索引号,但是这个失败了,我读了它不是最好的方法,所以现在我就这样了。
除此之外,我希望当我做line[1]
时,它会给我空字符串,但事实并非如此。
我在这里错过了什么?
只需使用re
split来获得多个char分隔符的优势:
>>> import re
>>>
>>> line = 'Some Name__________2.0 2.0 1.3\n'
>>> re.split(r'_+', line)
['Some Name', '2.0 2.0 1.3\n']
for 循环中的示例:
>>> lines = ['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n']
>>> for dat in [re.split(r'_+|\n', line) for line in lines]:
... person = dat[0]
... id = dat[1]
... print person, id
...
Some Name 2.0 2.0 1.3
Some Name 1.0 9.0 1.
使用列表推导删除空字符串。
for line in read_file:
tokens = [x for x in line.split("_") if x != ""]
person_name = tokens[0]
str.split
的输出是一个list
list
没有split
方法,这就是你得到那个错误的原因。
你可以这样做:
with open('yourfile') as f:
for line in f:
split = line.split('_')
name, number = split[0], split[-1]
print '{}-{}'.format(number, name)
有几点需要注意:
1)不要使用驼峰盒
2)使用文件的上下文管理器,也就是with
语句,如果出现故障,它会很好地处理文件状态
3)注意这一行: for line in f:
。 它具有遍历每一行的好处,永远不会将整个文件放在内存中
你可以这样做:
for line in readFile:
line = line.split("_")
line = filter(bool, line)
这将删除line
列表中的所有空字符串。
>>> a =['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n']
>>> import re
>>> [re.search(r'_+(.+)$', i.rstrip()).group(1) for i in a]
['2.0 2.0 1.3', '1.0 9.0 1.0']
readfile=['Some name____2.0 2.1 1.3','Some other name_____2.2 3.4 1.1']
data=[]
for line in readfile:
first_split=list(part for part in line.split('_') if part!='')
data.append(list([first_split [0],first_split [1].split(' ')]))
print(data)
如果我理解正确的话,我想这就是你想要的。 打印出来:
[['Some name', ['2.0', '2.1', '1.3']], ['Some other name', ['2.2', '3.4', '1.1']]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.