【发布时间】:2021-12-04 19:06:17
【问题描述】:
我正在处理一个文本文件,在该文件中我正在为登录页面制作 dict 和 zip 函数。但由于某些原因,它不时给我 ValueError 。这通常发生在我手动编辑或删除文本文件中的数据时,如果我创建另一个文本文件但我不能继续这样做,它将被修复。所以请帮助我。
文本文件
shanm, @Yolz3345
Jiaern, @Valorant
Steve, ImaG@nius
代码
#FOR LOGIN DETAILS
list_un = []
list_ps = []
members_list = open("det.txt", "r")
for info in members_list:
a,b = info.split(", ")
b = b.strip()
list_un.append(a)
list_ps.append(b)
data = dict(zip(list_un, list_ps))
我时常遇到的错误
a,b = info.split(", ")
ValueError: not enough values to unpack (expected 2, got 1)
【问题讨论】:
-
好像有一行没有“,”
-
首先分配给一个变量 -
parts = info.split(", ")然后检查len(parts)如果它是 2 那么你可以使用a, b = parts
标签: python string dictionary text valueerror