简体   繁体   中英

TypeError : unhashable type: list?

I'm trying to write excel file using XLWT from a CSV file(zipcode to State) but when i open csv and take statename from there then get this error otherwise if i set some hardcode value then it's executing.

import xlwt
import csv
row = 1
excelFile = xlwt.Workbook(encoding='utf-8')
sheet1 = excelFile.add_sheet('SampleData')
sheet1.col(0).width = 5000
sheet1.col(1).width = 5000
sheet1.write(0, 0, "Zip")
sheet1.write(0, 1, "State")
zip = '43215'
with open("usZipToCity.csv", 'r', encoding='utf-8') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        if row:
            if zip in row:
                stateName = row[1]

sheet1.write(row, 0, zip)
sheet1.write(row, 1, stateName)
row += 1
excelFile.save("SampleData.xls")

Error Traceback:

Traceback (most recent call last):
  File "G:/scrapingtasks/toddkreal_property/Tester.py", line 47, in <module>
    sheet1.write(row, 0, zip)
  File "C:\Users\muhammadhamaadlatif\AppData\Local\Programs\Python\Python37-32\lib\site-packages\xlwt\Worksheet.py", line 1088, in write
    self.row(r).write(c, label, style)
  File "C:\Users\muhammadhamaadlatif\AppData\Local\Programs\Python\Python37-32\lib\site-packages\xlwt\Worksheet.py", line 1139, in row
    if indx not in self.__rows:
TypeError: unhashable type: 'list'

As Martijn pointed out, you are using row for two different purposes:

  • to hold a line of parsed csv ;
  • to hold the index of the next row of the Excel file you will write.

The quickest way to fix this is to use a different variable name for each purposes. For example, you can rename row to csvrow in lines 13, 14, 15 and 16 of the code you provided.

If you have more time, I suggest you take a look at the following code. It includes a few stylistic fixes to your intial implementation as well as a write_row() function you might find interesting.

import xwlt
import csv

from itertools import count

if __name__ == "__main__":
    # Create an Excel file
    EXCELFILE = xwlt.WorkBook(encoding="utf-8")
    # Setup a sheet
    SHEET = EXCELFILE.add_sheet('SampleData')
    SHEET.col(0).width = 5000
    SHEET.col(1).width = 5000

    # Define an easy way to write a row of data to SHEET
    ROW_IDX = count()
    def write_row(*args):
        """Writes a row of data into SHEET"""
        row_idx = next(ROW_IDX)
        for col_idx, arg in enumerate(args):
            SHEET.write(row_idx, col_idx, arg)

    # Write the header (first row)
    write_row("Zip", "State")

    # Set ZIP to the zip code we are interested in
    ZIP = '43215'

    # Parse the csv file
    with open("usZipToCity.csv", encoding="utf-8") as CSVFILE:
        for CSVROW in csv.reader(CSVFILE):
            if ZIP in CSVROW:
                # CSVROW[1] contains a state name
                write_row(ZIP, CSVROW[1])

    # Write the Excel file to disk
    EXCEFILE.save("SampleData.xls")

Note : it is not clear to me from your code whether you expect to find several matching rows in the csv file. If not, you should probably use break after the first match.

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