所以,我试图使用本书的代码形式:
www.ebooks-it.net/ebook/practical-data-science-cookbook
这是这个:
我是python的新手,所以我不确定我是否正确使用它。我试着这样做:
import numpy
import pandas
import networkx as nx
import unicodecsv as csv
path = 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)
但是我没有像书中那样获得相同的输出。
我的输出是:
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]
答案 0 :(得分:2)
我猜你的示例代码中有一点错误。最后一行应为graph_from_csv("hero_network.csv")
(而非data
,而不是DataFrame
)?
这解释了TypeError
输出(因为您稍后尝试使用此open()
作为参数,其中函数期望文件的路径为字符串)。
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("hero-network.csv")
Out[15]: 167219