我想从文件中获取数字并跳过任何字符串,之后我想在一个列表中添加每个数字.在python中



这是我正在读取的文件我只是想从文件中提取数字,并把它放在一个列表中,而不带任何字符串。

Salem Al Rashed
1200
Sara Al Kandari
950
Ahmad Al Fadli
1550

这是我写的代码,但是不起作用

my_list = []
file = open("employees.txt","r")    
for lines in file:
if lines == ""
slicing = int[lines]

你可以这样做:

f = open("employees.txt","r")
employees = f.read().split("n")
f.close()
nums = []
for e in employees:
try:
nums.append(int(e))
except ValueError:
pass
print(nums)

看看这篇文章,了解如何用python解析文件:https://www.vipinajayakumar.com/parsing-text-with-python/

最新更新