⑴ python逐行讀取txt文件 每行為一個list
#!/usr/bin/envPython
#coding=utf-8
importre
#你的文件路徑
path="./tags.txt"
#讀取文件
file=open(path,encoding="utf-8")
#定義一個用於切割字元串的正則
seq=re.compile("s+")
result=[]
#逐行讀取
forlineinfile:
lst=seq.split(line.strip())
item={
"name":lst[0],
"val":lst[1:]
}
result.append(item)
#關閉文件
file.close()
print(result)
#輸出結果類似:
[
{
"name":1,
"val":["v1","v2"]
},
{
"name":2,
"val":["v1","v2"]
}
]