简体   繁体   English

用于写入文件的 UnicodeDecodeError

[英]UnicodeDecodeError for writing file

I know that this is a very common error, but it's the first time I've encountered it when trying to write a file.我知道这是一个非常常见的错误,但这是我第一次在尝试写入文件时遇到它。

I'm using networkx to work with graphs for network analysis, and when I try to write into any format:我正在使用networkx处理用于网络分析的图形,当我尝试写入任何格式时:

nx.write_gml(G, "Graph.gml")
nx.write_pajek(G, "Graph.net")
nx.write_gexf(G, "graph.gexf")

I get:我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 2, in write_pajek
  File "/Library/Python/2.7/site-packages/networkx/utils/decorators.py", line 263, in _open_file
    result = func(*new_args, **kwargs)
  File "/Library/Python/2.7/site-packages/networkx/readwrite/pajek.py", line 100, in write_pajek
    path.write(line.encode(encoding))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 19: ordinal not in range(128)

I haven't found documentation on this, so quite confused.我还没有找到这方面的文档,所以很困惑。

Wondering if you can make use of codec module to solve it or not.想知道您是否可以使用编解码器模块来解决它。 Just create a file object by codec as following before feeding to networkx.在提供给networkx之前,只需按如下方式通过编解码器创建一个文件对象。

ex,前任,

import codecs
f = codecs.open("graph.gml", "w", "utf-8")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM