gpt4 book ai didi

python - Xlsxwriter Python3 错误

转载 作者:行者123 更新时间:2023-12-04 20:33:49 25 4
gpt4 key购买 nike

我的 python3 脚本在关闭 xlsxwriter 方面遇到了问题。我包含了 workbook.close(),但这似乎会导致某种错误。有谁知道问题出在哪里?

import xlsxwriter
import statistics

workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})

power = []

for row in list:
power.append(row)
worksheet.write(i, col, row)
col += 1

worksheet.write(i, col, statistics.median(power))

workbook.close()

Traceback (most recent call last):
File "example.py", line 71, in <module>
workbook.close()
File "/usr/local/lib/python3.4/dist-packages/xlsxwriter/workbook.py", line 311, in close
self._store_workbook()
File "/usr/local/lib/python3.4/dist-packages/xlsxwriter/workbook.py", line 619, in _store_workbook
xml_files = packager._create_package()
File "/usr/local/lib/python3.4/dist-packages/xlsxwriter/packager.py", line 139, in _create_package
self._write_shared_strings_file()
File "/usr/local/lib/python3.4/dist-packages/xlsxwriter/packager.py", line 286, in _write_shared_strings_file
sst._assemble_xml_file()
File "/usr/local/lib/python3.4/dist-packages/xlsxwriter/sharedstrings.py", line 54, in _assemble_xml_file
self._write_sst_strings()
File "/usr/local/lib/python3.4/dist-packages/xlsxwriter/sharedstrings.py", line 84, in _write_sst_strings
self._write_si(string)
File "/usr/local/lib/python3.4/dist-packages/xlsxwriter/sharedstrings.py", line 96, in _write_si
string = re.sub('(_x[0-9a-fA-F]{4}_)', r'_x005F\1', string)
File "/usr/lib/python3.4/re.py", line 179, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: can't use a string pattern on a bytes-like object

最佳答案

我在示例中添加了一些变量初始化以使其能够编译并且它似乎在 Python 2 和 3 上正确运行:

import xlsxwriter
import statistics

workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})

power = []

# I added these variables to the OP program.
list = [1, 2, 3, 4, 5]
i = 0
col = 0

for row in list:
power.append(row)
worksheet.write(i, col, row)
col += 1

worksheet.write(i, col, statistics.median(power))

workbook.close()

输出:

enter image description here

我想这与您的示例之间的区别在于列表数组的内容。

您能否用一个演示实际问题的示例来更新您的问题?

关于python - Xlsxwriter Python3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45476062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com