[英]How can I create a Class in Python3 using OOP to load and read data from an Excel file?
[英]I need to load data to my python3 program from a file
我需要将数据从文件加载到我的 python 程序。 你能告诉我什么是最简单的方法吗?
该文件是 user_id.txt,内容是:
['1668938807', '8646077542', '2926881681', '634754486']
我也尝试过但没有奏效的是:
with open('user_id.txt', 'r') as f:
data = f.readlines()
lines = []
for line in data:
content = line.split(',')
for el in content:
new_el = el.strip('\'')
print(new_el)
下面(使用文件内容可以使用ast直接加载到列表中的事实)
import ast
with open('user_id.txt') as f:
lst = ast.literal_eval(f.read())
print(lst)
output
['1668938807', '8646077542', '2926881681', '634754486']
如果你想将数字加载到数组中,你可以试试这个:
numbers = []
with open("user_id.txt","r") as f:
data = f.readlines()
for line in data:
stripped = ex[1:-1] #stripped bracelets
numbers.append([ int(''.join(filter(str.isdigit, num))) for num in stripped.split(",") ] )
无论如何,您应该考虑使用 json 希望我回答了您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.