简体   繁体   English

NoneType: TypeError: 'tuple' object 不支持项目分配

[英]NoneType: TypeError: 'tuple' object does not support item assignment

I am reading an Excel file with openpyxl and rows are getting parsed as tuple objects.我正在阅读带有 openpyxl 的 Excel 文件,并且行被解析为元组对象。 One of the objects in the tuple can be a NoneType.元组中的对象之一可以是 NoneType。 Further in the code rows are compared with other rows.进一步将代码行与其他行进行比较。 These last rows don't have a NoneType, but are a empty String object, which hampers the comparison.最后这些行没有 NoneType,而是一个空字符串 object,这妨碍了比较。 So I'm using the following code to replace the NoneType by an empty String object.因此,我使用以下代码将 NoneType 替换为空字符串 object。

                for row in previous_worksheet.iter_rows(values_only=True):
                    print("row " + str(type(row)))
                    for x in range(len(list(row))):
                        print(type(row[x]))
                        if row[x] is None:
                            print(row[x])
                            row[x] = ""

Running this code gives me the following output:运行此代码会给我以下 output:

row <class 'tuple'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'NoneType'>
None
Traceback (most recent call last):
  File "C:\PycharmProjects\SecuniaComparer\secunia_comparer.py", line 187, in <module>
    main()
  File "C:\PycharmProjects\SecuniaComparer\secunia_comparer.py", line 182, in main
    sc.start_parsing()
  File "C:\PycharmProjects\SecuniaComparer\secunia_comparer.py", line 71, in start_parsing
    row[x] = ""
TypeError: 'tuple' object does not support item assignment

The object is a NoneType, but assigning an empty String results in a TypeError for a tuple object. object 是 NoneType,但分配空字符串会导致元组 object 出现 TypeError。 How can I fix this?我怎样才能解决这个问题?

My bad, creating a new list object solves the issue.我的错,创建一个新列表 object 解决了这个问题。

                    temp_row = list(row)
                    for x in range(len(temp_row)):
                        if temp_row[x] is None:
                            temp_row[x] = ("")
                    self.previous_results.append(tuple(temp_row))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:“ NoneType”对象不支持项目分配 - TypeError: 'NoneType' object does not support item assignment TypeError:'NoneType'对象不支持项目分配? - TypeError: 'NoneType' object does not support item assignment? TypeError:“元组”对象不支持项目分配 - TypeError: 'tuple' object does not support item assignment TypeError:“元组”对象不支持项目分配在非元组对象上 - TypeError: 'tuple' object does not support item assignment On non tuple object 如何修复“TypeError: 'NoneType' object 不支持项目分配” - How To Fix “TypeError: 'NoneType' object does not support item assignment” 机械化:TypeError:&#39;NoneType&#39; 对象不支持项目分配 - Mechanize: TypeError: 'NoneType' object does not support item assignment 如何修复 TypeError:&#39;NoneType&#39; 对象不支持项目分配 - how to fix TypeError: 'NoneType' object does not support item assignment Appengine:TypeError:&#39;NoneType&#39;对象不支持项目分配 - Appengine: TypeError: 'NoneType' object does not support item assignment 列表 object 上的“TypeError: 'tuple' object 不支持项目分配” - “TypeError: 'tuple' object does not support item assignment” on a list object 如何修复:TypeError &#39;tuple&#39; 对象不支持项目分配 - How to fix a : TypeError 'tuple' object does not support item assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM