问题
I'm analizing Marvels Social Network I have this code:
import numpy
import pandas
import networkx as nx
import unicodecsv as csv
data = pandas.read_csv('hero-network.csv', low_memory=False)
def graph_from_csv(path):
graph = nx.Graph(name="Heroic Social Network")
with open(path, 'rU') as data:
reader = csv.reader(data)
for row in reader:
graph.add_edge(*row)
return graph.size()
graph_from_csv(data)
I'm new to python. And I'm not quite sure why my code has this kind of output insted of the graphs size:
TypeError: invalid file: LITTLE, ABNER PRINCESS ZANDA
0 LITTLE, ABNER BLACK PANTHER/T'CHAL
1 BLACK PANTHER/T'CHAL PRINCESS ZANDA
2 LITTLE, ABNER PRINCESS ZANDA
3 LITTLE, ABNER BLACK PANTHER/T'CHAL
4 BLACK PANTHER/T'CHAL PRINCESS ZANDA
5 STEELE, SIMON/WOLFGA FORTUNE, DOMINIC
6 STEELE, SIMON/WOLFGA ERWIN, CLYTEMNESTRA
7 STEELE, SIMON/WOLFGA IRON MAN/TONY STARK
8 STEELE, SIMON/WOLFGA IRON MAN IV/JAMES R.
9 STEELE, SIMON/WOLFGA RAVEN, SABBATH II/EL
10 RAVEN, SABBATH II/EL FORTUNE, DOMINIC
11 RAVEN, SABBATH II/EL ERWIN, CLYTEMNESTRA
12 RAVEN, SABBATH II/EL IRON MAN/TONY STARK
13 RAVEN, SABBATH II/EL IRON MAN IV/JAMES R.
14 IRON MAN IV/JAMES R. FORTUNE, DOMINIC
15 IRON MAN IV/JAMES R. ERWIN, CLYTEMNESTRA
16 IRON MAN IV/JAMES R. IRON MAN/TONY STARK
17 IRON MAN/TONY STARK FORTUNE, DOMINIC
18 IRON MAN/TONY STARK ERWIN, CLYTEMNESTRA
19 ERWIN, CLYTEMNESTRA FORTUNE, DOMINIC
20 PRINCESS ZANDA BLACK PANTHER/T'CHAL
21 PRINCESS ZANDA LITTLE, ABNER
22 LITTLE, ABNER BLACK PANTHER/T'CHAL
23 BLACK PANTHER/T'CHAL LITTLE, ABNER
24 BLACK PANTHER/T'CHAL PRINCESS ZANDA
25 BLACK PANTHER/T'CHAL CARNIVORE/COUNT ANDR
26 BLACK PANTHER/T'CHAL LITTLE, ABNER
27 LITTLE, ABNER PRINCESS ZANDA
28 LITTLE, ABNER CARNIVORE/COUNT ANDR
29 CARNIVORE/COUNT ANDR PRINCESS ZANDA
... ...
574436 CANNONBALL II/SAM GU WASP/JANET VAN DYNE
574437 WASP/JANET VAN DYNE ROGUE /
574438 WASP/JANET VAN DYNE CALIBAN/
574439 WASP/JANET VAN DYNE CALLISTO
574440 WASP/JANET VAN DYNE COLOSSUS II/PETER RA
574441 WASP/JANET VAN DYNE SELENE
574442 WASP/JANET VAN DYNE PROFESSOR X/CHARLES
574443 WASP/JANET VAN DYNE PHOENIX III/RACHEL S
574444 PHOENIX III/RACHEL S ROGUE /
574445 PHOENIX III/RACHEL S CALIBAN/
574446 PHOENIX III/RACHEL S CALLISTO
574447 PHOENIX III/RACHEL S COLOSSUS II/PETER RA
574448 PHOENIX III/RACHEL S SELENE
574449 PHOENIX III/RACHEL S PROFESSOR X/CHARLES
574450 PROFESSOR X/CHARLES ROGUE /
574451 PROFESSOR X/CHARLES CALIBAN/
574452 PROFESSOR X/CHARLES CALLISTO
574453 PROFESSOR X/CHARLES COLOSSUS II/PETER RA
574454 PROFESSOR X/CHARLES SELENE
574455 SELENE ROGUE /
574456 SELENE CALIBAN/
574457 SELENE CALLISTO
574458 SELENE COLOSSUS II/PETER RA
574459 COLOSSUS II/PETER RA ROGUE /
574460 COLOSSUS II/PETER RA CALIBAN/
574461 COLOSSUS II/PETER RA CALLISTO
574462 CALLISTO ROGUE /
574463 CALLISTO CALIBAN/
574464 CALIBAN/ ROGUE /
574465 HULK/DR. ROBERT BRUC MARKS, DR. SHIELA
[574466 rows x 2 columns]
What am I doing wrong, how can I make the code to output the size of the graph. And also I have an type error but the same csv file is used in the manual where I found this code and they aren't getting the type error, but just the output with the numeric size of the graph.
来源:https://stackoverflow.com/questions/35294519/analizing-marvels-universe-social-network-python