所以我正在使用 win32com.client 模块做一些工作。我做了一些网络抓取来制作所有大量枚举的数据框。网络抓取成功,但除非我注释掉该行,否则一个模块将无法加载。
我做了一个gist代码的一部分,因为有几行几乎有 13k 个字符长。
我尝试导入office_reverse_enumerations.py时出现的错误是
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xe2 in position 0: unexpected end of data
在第 17 行。如果我注释掉第 17 行,它就可以正常运行。
我以编程方式生成了该行,以及所有行。
一个示例写入是
enum_file.write(f"{enum_names[idx][df_idx]}_reverse = pd.DataFrame.from_dict({df_r_enum.to_dict()})\n")
>>> import office_reverse_enumerations
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\sdfg-Mobile\Documents\Gits\power_point_generator\office_reverse_enumerations.py", line 17
^
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xe2 in position 0: unexpected end of data
Here是我用来生成文件的代码。
最佳答案
当使用open
写入文本文件而不指定编码
时,将使用系统相关的默认编码(可以使用locale.getpreferredencoding检索) (错误)
)。
Python 代码文件应采用 UTF-8 编码,因此解决方案是在编写代码时在 open
函数中显式指定 encoding="utf-8"
代码文件。
关于python - 代码行太长,Python 无法解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810737/