UnicodeDecodeError用于写入文件 [英] UnicodeDecodeError for writing file

查看:89
本文介绍了UnicodeDecodeError用于写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常常见的错误,但这是我第一次尝试写入文件时遇到的错误.

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.

我正在使用networkx来处理用于网络分析的图形,并且当我尝试以任何格式书写时:

I'm using networkx to work with graphs for network analysis, and when I try to write into any format:

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

我得到:

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.

推荐答案

想知道是否可以使用 codec 模块来解决它.只需通过编解码器按照以下方式创建文件对象,然后再馈入networkx.

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.

例如

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

这篇关于UnicodeDecodeError用于写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆