需求描述
有以下文件 info.txt
, 想要讀取其中的 fileVideoId, 組成一個列表后返回.文章來源:http://www.zghlxwxcb.cn/news/detail-709396.html
[{"source":1,"fileVideoId":10001,"videoId":"ks009182837mgsciro","materialId":190929}]
[{"source":1,"fileVideoId":10002,"videoId":"ks009182837mgsciro","materialId":190930}]
[{"source":1,"fileVideoId":10003,"videoId":"ks009182837mgsciro","materialId":190931}]
[{"source":1,"fileVideoId":10004,"videoId":"ks009182837mgsciro","materialId":190932}]
代碼
import json
json_path = "info.txt"
fileVideoIds = []
# 讀取 json 日志文件
with open(json_path, 'r') as load_f:
# 讀取文件
lines = load_f.readlines()
# 處理每一行的 json 內容
for line in lines:
# 將數(shù)據(jù)轉為 json
arr = json.loads(line)
# 處理數(shù)組中每一個元素
for data in arr:
fileVideoIds.append(data.get('fileVideoId'))
print(fileVideoIds)
運行結果:文章來源地址http://www.zghlxwxcb.cn/news/detail-709396.html
[10001, 10002, 10003, 10004]
到了這里,關于python 讀取文件, 轉化為 json 格式, 獲取 json 中某個屬性的值的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!