无法使用Python从文件中读取所有数据
在 test.txt 中有:
{'God': {u'user1@localhost': {}, u'user2@localhost': {}, u'user3@localhost': {}}}
代码是:
# coding: utf-8
def read_file(filename):
data=None
try:
fp = file(filename)
data = fp.read()
fp.close()
except: pass
return data
def read():
file = 'test.txt'
db = eval(read_file(file))
if "God" in db:
for x in db["God"]:
data = x
#print(x) $$ it'll print all data True but I do not need it, I will put instructions inside and I do not need to be repeated.
print(x) # $$ print just 1 data from file
try: read()
except: pass
如何让它读取文件中的所有数据
谢谢。
最佳答案
要从文本文件中读取所有数据,只需使用 read文件对象的方法,例如
with open('test.txt','r') as f:
file_content = f.read()
关于python - 使用python从文件中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28314186/