Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中拆分列表时如何去除空字符串?_Python - Fatal编程技术网

在Python中拆分列表时如何去除空字符串?

在Python中拆分列表时如何去除空字符串?,python,Python,我有一个输入文件,它由以下几行组成: ['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n', # and so on....] 我已使用读线将其格式化为: ['Some Name', '', '', '', '2.0 2.0 1.3\n'] ['Another Name', '', '', '', '1.0 9.0 1.0\n'] ['Another Name', '', '', '', '1.0 9.0

我有一个输入文件,它由以下几行组成:

['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n', # and so on....]
我已使用
读线
将其格式化为:

['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'
我该怎么做?我想学这个

我也尝试过增加索引号,但是失败了,我读到这不是最好的方法,所以现在我要这样做

除此之外,我希望当我执行
行[1]
时,它会给我空字符串,但它不会


这里我遗漏了什么?

str.split的输出是一个
列表

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) 注意这一行:
对于f中的行:
。它的好处是迭代每一行,而不会将整个文件存储在内存中

您可以这样做:

for line in readFile:
    line = line.split("_")
    line = filter(bool, line)
这将删除
列表中的所有空字符串。

只需使用
re
即可利用多字符分隔符:

>>> 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循环中的示例:


使用列表理解删除空字符串

for line in read_file:
     tokens = [x for x in line.split("_") if x != ""]
     person_name = tokens[0]
如果我理解正确,我想这就是你想要的。它打印出:

[['Some name', ['2.0', '2.1', '1.3']], ['Some other name', ['2.2', '3.4', '1.1']]]

@Siyah它创建了一个由10个
\uu
字符组成的字符串。我试着用文字,因为使用正则表达式可能超出了这个问题的范围,但是为什么要创建一个包含10个字符的字符串呢?如果我有一个11个字符的文件,那意味着什么?这不会是一个通用的修复,对吗?哦,对不起,我会用一个通用的更新,我假设它是10修复chars@Siyah我知道你已经接受了答案,但请注意以下几点,以提高你的回答coding@Siyah这只是变量命名的python标准。如果你对PEP8感兴趣,你应该查看它,它有很多推荐(只需谷歌一下)谢谢。我不会用这个,但是知道有一个替代品是很好的,很有趣。不是我要找的那个,因为我想使用for循环,但是很有趣。谢谢。它很容易适应for循环,检查我编辑的答案。我认为OP真的在寻找一本字典,这个答案可以很容易地适应。嗯,它似乎没有做我需要的。无论如何谢谢你!是的,这就是我需要的。。。谢谢你,伙计。最后一个问题:我还有别的办法吗。。。我的意思是,我想要的是得到数字。。。如何单独获取数字?然后您可以在数据的附加部分中省略
第一次拆分[0]
部分,只使用
第一次拆分[1]。拆分(“”)
该行类似于:
数据。追加(列表([first\u split[1].split(“”)])
?如果只需执行列表压缩,为什么要从生成器实例化列表?
for line in read_file:
     tokens = [x for x in line.split("_") if x != ""]
     person_name = tokens[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']]]