python - 代码行太长,Python 无法解释?

标签 python

所以我正在使用 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/

相关文章:

python - python中直方图的概率密度函数以拟合另一个直方图

python - 编码问题-Python

python - 杂波不会在断点之外更新屏幕

python - 检查套接字端口是否可用

python - 使用cv2在python中在手绘图像上连接角的线

python - 在 while 循环内延迟 (Python/Pygame)

python - 连接具有不同第一维的二维数组

python struct.pack 和 write 与 matlab fwrite

python - lxml 的 iterparse 中有多个标签名称?

python - 如何解释 tf.layers.dropout 训练参数