1

I am trying to read in a graphml file of my facebook network into NetworkX. However, because some of my friends have unusual characters, such as accents, their names are unable to be read into networkx.

I ran the command:

g = nx.read_graphml("/Users/juliehui/Desktop/MyGraph.graphml")

I then get the error:

TypeError: int() argument must be a string or a number, not 'NoneType'

I looked at the graphml file in Sublime Text, and it seems to have trouble with names, such as Andrés

I then looked at the graphml file in Gephi to see what it looked like. The name, Andrés, in Gephi looks like:

Andrés

When I export the data without making any edits into a separate graphml file, and try to read that file in, I get the error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)

When I delete the problem names in Gephi, then the file reads fine.

I am not sure if there is some way to edit my original graphml file to fix the names with unusual characters.

I have looked at this page: Graphml parse error But, I could not figure out if my graphml file is in UTF-8 or needs to be in UTF-8 or needs to be in ASCII?

I have also tried:

data="/Users/juliehui/Desktop/MyGraph.graphml"
udata=data.decode("utf-8")
asciidata=udata.encode("ascii","ignore")
g = nx.read_graphml(asciidata)

But, this gave the error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-19: ordinal not in range(128)

How do I resolve this error?

4

2 回答 2

4

这在 Python 2.7 中对我有用。您必须将节点类型指定为 unicode。

nx.read_graphml('/path/to/my/file.graphml', unicode)
于 2014-04-15T22:24:20.187 回答
1

我建议使用unidecode删除文件中的所有非 ASCII 字符:

from unidecode import unidecode
data_in="/Users/juliehui/Desktop/MyGraph.graphml"
data_ascii ="/Users/juliehui/Desktop/MyGraph_ASCII.graphml"
f_in = open(data_in, 'rb')
f_out = open(data_ascii, 'wb')
for line in f_in:
    f_out.write(unidecode(line))
f_in.close()
f_out.close()

然后你可以希望使用:

g = nx.read_graphml(data_ascii)
于 2013-07-22T12:59:25.507 回答