#!/usr/bin/env Python
# coding=utf-8
import re
# 你的檔案路徑
path = "./tags.txt"
# 讀取檔案
file = open(path, encoding="utf-8")
# 定義一個用於切割字串的正則
seq = re.compile("\s+")
result = []
# 逐行讀取
for line in file:
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,
]
#!/usr/bin/env Python
# coding=utf-8
import re
# 你的檔案路徑
path = "./tags.txt"
# 讀取檔案
file = open(path, encoding="utf-8")
# 定義一個用於切割字串的正則
seq = re.compile("\s+")
result = []
# 逐行讀取
for line in file:
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"]
}
]