我正在阅读一个文本文件,其中包含以下格式的数据:
column : row
这是一些示例数据:
Name of the Property : North Kensington Upcycling Store and Cafe
Availability : Now
Interest Level : 74 people are looking right now
Area : 1,200 sqft
Retail Type : No
Bar & Restaurant Type : No
我的代码给出了这个错误:
ValueError: need more than 1 value to unpack
在这一行:
k,v = txt_line.split(":")
我的代码:
import pandas
txt_file = r"patty.txt"
txt = open(txt_file, "r")
txt_string = txt.read()
txt_lines = txt_string.split("\n")
txt_dict = {}
for txt_line in txt_lines:
print(txt_line)
k,v = txt_line.split(":")
k = k.strip()
v = v.strip()
if k in txt_dict:
list = txt_dict.get(k)
else:
list = []
list.append(v)
txt_dict[k]=list
print (pandas.DataFrame.from_dict(txt_dict, orient="index"))
答案 0 :(得分:1)
如果输入文件的一行为空或缺少冒号,var json_data = JSON.parse({{ vote_records }});
json_data.forEach(function(entry) {
console.log(entry);
});
只返回1个元素,则会出现该错误。
为了安全起见,我会进行大小检查以避免异常,并在无法解析时打印显式消息(我添加空行跳过以避免在这种情况下崩溃)
split