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?

Community
  • 1
  • 1
user2292210
  • 837
  • 1
  • 8
  • 8

2 Answers2

4

This worked for me in Python 2.7. You have to specify the node type as unicode.

nx.read_graphml('/path/to/my/file.graphml', unicode)
unzurdo
  • 51
  • 6
1

I would suggest to use unidecode to remove all non ASCII character in the file:

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()

Then you can hopefully use:

g = nx.read_graphml(data_ascii)
Bruno Gabuzomeu
  • 1,061
  • 8
  • 9