简体   繁体   中英

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. One of the objects in the tuple can be a 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. So I'm using the following code to replace the NoneType by an empty String 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:

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. How can I fix this?

My bad, creating a new list object solves the issue.

                    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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM