我还看到错误消息“valueerror cannot convert to excel”。我看过这个网站,似乎很多其他网站也收到了类似的错误消息。但是我的代码看起来不像他们的代码,并且不清楚他们提供的解决方案对我来说是否有意义。
The exact and complete error message reported is: ValueError: Cannot convert ['NHL Teams.zip', 'https://canvas.uw.edu/courses/1675266/files/folder/Lesson 05', 'https://canvas.uw.edu/courses/1675266/files/105188746/download?wrap=1', 'NHL', '', 'BOS.png', '', 'Unsupported file type: (.png) skipped in search.'] to Excel
I’m working on a program that loops over some data and saves the result to an Excel file that the program creates. I’ve been adding rows to the Excel file using the construction similar to:
WorkbookObject.append(list_of_strings_variable)
This works. In some cases though, I need to call a function that returns a list of (list of strings) and add the strings from the inner list to the end of an existing list and save the results as new rows in the spreadsheet.
To test my logic and the syntax I need to support it I wrote this small test program which models the data transformation I’m intending and, does save the contents to the spreadsheet.
Here is my test program:
import openpyxl
EXCEL_COL_NAMES = ['File Name',
'Path in Canvas',
'Download Link',
'Keywords found in Filename',
'Keywords found File Conents',
'Zipfile Member Name',
'Keword in Zip Member Name',
'Keword in Zip Member Contents']
def someFunction(n):
result = []
for i in range(n) :
result .append(['sixth', 'seventh', 'eighth'])
return result
wb = openpyxl.Workbook()
ws = wb.active
ws.append(EXCEL_COL_NAMES)
testData = someFunction(20)
for item in testData:
ws.append(['first', 'second', 'third', 'fourth', 'fifth' ] + item)
wb.save('Test.xlsx')
H
测试程序生成的Excel文件内容如下:
But when I include this logic in the larger program I’m writing the program crashes and reports the “valueerror cannot convert” message. Here is a fragment from the larger program:
elif (file_extension == '.zip'):
kw_in_content = zipSearch(downloaded_file_path, KEYWORDS_LIST, ZIP_FOLDER)
else:
kw_in_content = f'Unsupported file type: ({file_extension}) skipped in search.'
print(f'Keywords in File Content: {kw_in_content }')
"""
Now I'm ready to write the results of the search to the Excel file.
For most file types I'm writing one row per file and the last three fields, which contain
data about zip files, are unpopulated. For zip files I'm writing one row for each file inside
the zip file, including data in the columns specific to zip files.
"""
if not file_extension == '.zip':
results_list = [file.display_name, Files_section_url, file_download_url, kw_in_filename, kw_in_content, '', '', '']
else:
for zipfile_member in kw_in_content:
results_list.append([file.display_name, Files_section_url, file_download_url, kw_in_filename, '', ] + zipfile_member)
# These two print statements are for debugging only.
print(f'Type of list literal = ', type([file.display_name, Files_section_url, file_download_url, kw_in_filename, '', ] + zipfile_member))
print(f'zipfile_member = {zipfile_member} (type = {type(zipfile_member)}, results_list = {results_list}')
ws.append(results_list)
wb.save(EXCEL_FOLDER + '/' + EXCEL_FILENAME)
这是从我的函数“zipSearch”返回的一些示例数据,该函数正在创建(字符串列表)列表:
[['BOS.png', '', '不支持的文件类型:(.png) 在搜索中跳过。'], ['CHI.png', '', '不支持的文件类型:(.png) 在搜索中跳过.'], ['DAL.png', '', '不支持的文件类型:(.png) 在搜索中跳过。'], ['DET.png', '', '不支持的文件类型:(.png) 跳过在搜索中。'], ['NJD.png', '', '不支持的文件类型:(.png) 在搜索中跳过。'], ['SJS.png', '', '不支持的文件类型:(.png ) 在搜索中跳过。'], ['TBL.png', '', '不支持的文件类型:(.png) 在搜索中跳过。']]
All the data going into the Excel file is of type string. I’ve double and triple checked that the lists I’m sending to Excel always have the same number of strings. I’m not clear why openpyxl would have trouble writing some lists to Excel and not others.
我期待我的数据被写入 Excel 文件。
I'm not sure what else I should try. The smaller sample program models the data transformation I wanted and caused no trouble for the openpyxl module. The logic and syntax I'm using in the larger program should be identical from the perspective of the openpyxl module.
I don't think it should matter, from the perspective of the openpyxl module, how many values in the list I'm passing to Excel. But I checked that anyway.
I did add some print statements to examine the data going into this logic. Everything seemed in order there.