Python 2.7 写入gml文件时发生编码错误
在我以前的一篇文章中,我在读写与英语不同的字符串时遇到了问题。问题出在我的系统编码上ton1c提到在txt中编写字符串是很好的,事实上确实如此!现在我试图在gml文件中传递这些字符串,但我再次遇到编码问题。下面是代码和结果Python 2.7 写入gml文件时发生编码错误,python-2.7,encoding,networkx,Python 2.7,Encoding,Networkx,在我以前的一篇文章中,我在读写与英语不同的字符串时遇到了问题。问题出在我的系统编码上ton1c提到在txt中编写字符串是很好的,事实上确实如此!现在我试图在gml文件中传递这些字符串,但我再次遇到编码问题。下面是代码和结果 import urllib2 import BeautifulSoup import networkx as nx url = 'http://www.bbc.co.uk/zhongwen/simp/' page = urllib2.urlopen(url).read()
import urllib2
import BeautifulSoup
import networkx as nx
url = 'http://www.bbc.co.uk/zhongwen/simp/'
page = urllib2.urlopen(url).read().decode("utf-8")
dom = BeautifulSoup.BeautifulSoup(page)
data = dom.findAll('meta', {'name' : 'keywords'})
data = data.encode("utf-8")
datalist = data.split(',')
G = nx.Graph()
G.add_node( "name", Strings = datalist );
它回来了
File "C:\...\name.py", line 23, in <module> nx.write_gml(G, 'Gname')
File "<string>", line 2, in write_gml
File "C:\Python27\lib\site-packages\networkx\utils\decorators.py", line 263, in _open_file
result = func(*new_args, **kwargs)
File "C:\Python27\lib\site-packages\networkx\readwrite\gml.py", line 392, in write_gml
path.write(line.encode('latin-1'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 13: ordinal not in range(128)
文件“C:\…\name.py”,第23行,在nx.write\u gml(G,'Gname')中
文件“”,第2行,以写入方式
文件“C:\Python27\lib\site packages\networkx\utils\decorators.py”,第263行,在\u open\u文件中
结果=func(*新参数,**kwargs)
文件“C:\Python27\lib\site packages\networkx\readwrite\gml.py”,第392行,在write\u gml中
path.write(line.encode('latin-1'))
UnicodeDecodeError:“ascii”编解码器无法解码第13位的字节0xe4:序号不在范围内(128)
有什么建议吗?我还想提到的是,在networkx网站中,它提到了GML规范,指出该文件应仅使用7bit ASCII文本编码。iso8859-1(拉丁语-1)。()
PS:请提供Python 2.7兼容性方面的任何建议。您只需执行以下操作:
import urllib2
import BeautifulSoup
import networkx as nx
url = 'http://www.bbc.co.uk/zhongwen/simp/'
page = urllib2.urlopen(url).read().decode("latin-1")
dom = BeautifulSoup.BeautifulSoup(page)
data = dom.findAll('meta', {'name' : 'keywords'})
data = data[0]['content'].encode("latin-1")
#datalist = data.split(',')
with open("tags.txt", "w") as text_file:
text_file.write("%s"%data)
G = nx.Graph()
G.add_node( "name", Strings = data.decode("latin-1") );
nx.write_gml(G,"test.gml")
graph [
node [
id 0
label "name"
Strings "BBC中文网,主页,国际新闻,中国新闻,台湾新闻,香港新闻,英国新闻,信息,财经,科技,卫生 互动,多媒体,视频,音频,图辑,bbcchinese.com, homepage, world news, China news, uk news, hong kong, taiwan, sci-tech, business, interactive, forum"
]
]
您只需执行以下操作:
import urllib2
import BeautifulSoup
import networkx as nx
url = 'http://www.bbc.co.uk/zhongwen/simp/'
page = urllib2.urlopen(url).read().decode("latin-1")
dom = BeautifulSoup.BeautifulSoup(page)
data = dom.findAll('meta', {'name' : 'keywords'})
data = data[0]['content'].encode("latin-1")
#datalist = data.split(',')
with open("tags.txt", "w") as text_file:
text_file.write("%s"%data)
G = nx.Graph()
G.add_node( "name", Strings = data.decode("latin-1") );
nx.write_gml(G,"test.gml")
graph [
node [
id 0
label "name"
Strings "BBC中文网,主页,国际新闻,中国新闻,台湾新闻,香港新闻,英国新闻,信息,财经,科技,卫生 互动,多媒体,视频,音频,图辑,bbcchinese.com, homepage, world news, China news, uk news, hong kong, taiwan, sci-tech, business, interactive, forum"
]
]