請大大幫忙解答,我讀取一個記事本com.txt,該怎麼寫依index讀取
com.txt文本資料['https://i.imgur.com/yCRKRrW.jpg', 'https://i.imgur.com/Wi2ZpI0.jpg', 'https://i.imgur.com/0o3RywQ.jpg', 'https://i.imgur.com/J32O3Y6.jpg', 'https://i.imgur.com/3uYICfO.jpg', 'https://i.imgur.com/NtzTFsY.jpg', 'https://i.imgur.com/1gZmY1u.jpg', 'https://i.imgur.com/tJM6E1b.jpg', 'https://i.imgur.com/OJjyDbs.jpg', 'https://i.imgur.com/xV1SDLT.jpg', 'https://i.imgur.com/gxsGgpg.jpg', 'https://i.imgur.com/YrZbHmU.jpg']
file = 'com.txt'
with open(file, 'r') as f:
a = f.read()
使用print(a[0]) 輸出只有 [ 符號 ,無法輸出圖片網址
該怎麼轉換可以正常使用list,麻煩大大們幫忙
import ast
file = 'com.txt'
with open(file, 'r') as f:
a = f.read()
urlList = ast.literal_eval(a)
print(urlList[0])
提供用JSON去做的方法
import json
file = 'com.txt'
with open(file, 'r') as f:
a = f.read()
data = json.loads(a.replace("'", '"'))
print(data[0])