【发布时间】:2020-06-21 20:08:45
【问题描述】:
我正在尝试编写一段代码,它将获取字符串中的某些值并输出它们。问题是我正在使用的数据集并不完美,并且有很多部分数据是空的。我正在寻找一种方法让 python 忽略空白值并继续前进
rfile = open("1.txt", "r", encoding= "utf8")
combos = rfile.readlines()
a=0
nfile = open("2.txt ", "w+")
numx = 0
for line in combos:
x = combos[a]
y=(x.split('Points = '))
z= int(y[-1])
numx += 1
print (z)
print (numx)
rfile.close()
nfile.close()
exi = input("Press any key to close")
数据集示例如下:
Person 1 | Points = 22
Person 2 | Points = <--- This is the problematic data
Person 3 | Points = 15
任何帮助将不胜感激!
【问题讨论】:
-
澄清一下,我想让python检查z是否为空,如果是就忽略它。
-
if z:应该可以工作。 -
你可以检查
y[-1]是否为空字符串""。 -
z= y[-1] != ''? int(y[-1]) : ''