1. 讀取TXT檔案
In [3]:
import jsondef open_file():
file = open("credential.txt","r")
js = file.read()
dic = json.loads(js)
file.close()
return dicopen_file()
Out[3]:
{'user': '吳披薩', 'id': '10257515', 'passwd': 'abcdefg'}
In [5]:
def open_file():
file = open("credential.txt","r+")
dic = eval(file.read())
file.close()
return dica = open_file()
new_json = {
"user":a["user"],
"id":a["id"]
}print(new_json){'user': '吳披薩', 'id': '10257515'}
2. 使用 os.read() 讀取
In [9]:
import os, sys, json
# 打开文件
fd = os.open("credential.txt",os.O_RDWR)
# 读取文本
#ret = os.read(fd,12)
ret = os.read(fd,100)
dic = json.loads(ret)
print(dic)# 关闭文件
os.close(fd){'user': '吳披薩', 'id': '10257515', 'passwd': 'abcdefg'}