繁体   English   中英

我想从文件中获取数字并跳过任何字符串,之后我想将每个数字附加到一个列表中。 在蟒蛇

[英]I want to take numbers from a file and skip any string, after that i want to append every number inside one list. in 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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM