python - ValueError:字典更新序列元素 #13 的长度为 1;2 是必需的
问题描述
我收到以下错误:
ValueError Traceback (most recent call last)
<ipython-input-19-ec485c9b9711> in <module>
31 except Exception as e:
32 print(e)
---> 33 raise e
34 print(i)
35 i = i+1
<ipython-input-19-ec485c9b9711> in <module>
21 # cc = dict(x.split(':') for x in c.split(','))
22 c = '"'.join(c)
---> 23 cc = dict(x.split(':') for x in c.split(','))
24 df_temp = pd.DataFrame(cc.items())
25 df_temp = df_temp.replace('"','',regex=True)
ValueError: dictionary update sequence element #13 has length 1; 2 is required
下面是抛出错误的块。我在这里查看了一些帖子,但它们是特定于代码的。不确定是输入问题还是代码。
df_final = pd.DataFrame()
i=1
for file in files:
try:
s3 = session.resource('s3')
key = file
obj = s3.Object('my-bucket',key)
n = obj.get()['Body'].read()
gzipfile = BytesIO(n)
gzipfile = gzip.GzipFile(fileobj=gzipfile)
content = gzipfile.read()
content = content.decode('utf-8')
if len(content) > 0:
content = re.findall(r"(?<=\{)(.*?)(?=\})",content)
for c in content:
c= c.split('"')
for index,val in enumerate(c):
if index%2 == 1:
c[index] = val.replace(':','_').replace(',','_')
c = '"'.join(c)
cc = dict(x.split(':') for x in c.split(','))
df_temp = pd.DataFrame(cc.items())
df_temp = df_temp.replace('"','',regex=True)
df_temp = df_temp.T
new_header = df_temp.iloc[0] #grab the first row for the header
df_temp = df_temp[1:] #take the data less the header row
df_temp.columns = new_header
df_final = pd.concat([df_final, df_temp])
except Exception as e:
print(e)
raise e
print(i)
i = i+1
你能分享这里的问题吗?这以前可以正常工作。我是进行更改还是忽略错误?
解决方案
我的猜测是您的数据格式不正确。我猜在某个时候,x.split(':')
正在生成一个只有一个元素的列表,因为没有:
in x
,字符串被拆分。这导致,在从该数据创建字典期间,当需要一对值(用于“键”和“值”)时传递单个值。
我建议您启动调试器,或者让调试器在遇到此错误时停止,或者弄清楚它何时发生并到达您即将发生错误的地步。然后查看调试器显示中正在或即将处理的数据,看看是否能找到导致问题的格式错误的数据。在运行引发异常的行之前,您可能必须对数据运行 prep pass 以解决此问题和其他类似问题。
推荐阅读
- latex - Google Colaboratory 上的 MathJax v3
- javascript - 如何在 Electron + React App 中创建路由
- reactjs - 如何使用 AWS Cognito 上的未经身份验证的用户在 React 上启用 AWS 位置服务地图?
- jmeter-plugins - 分析侦听器的多个结果以提高性能
- python - 将静态文件上传到 aws 后,Summernote 图标未加载
- angular - 提交表单时也会显示错误的角度材质自定义控件
- laravel - laravel when() 辅助函数在值为 false 时执行函数
- mysql - MySQL Query with Left Join、Order By 和 Limit 以及其他连接
- rundeck - 绕过 Rundeck 中的默认节点执行程序
- javascript - Treat inline script tags with type="text/plain" as JavaScript