Python:如何将 pickled txt 文件转换为 gpickles for networkx?
Python: how to convert pickled txt files into gpickles for networkx?
在处理同一图表 G
的多个不同实例后,我使用以下行将它们转储为 txt
文件 pickle
:
pickling=pickle.dump(G,open('pickled_G.txt','w')) #Example for one single graph
现在,为了进一步计算,我想通过执行以下操作将这些图加载回 networkx:
work_dir=raw_input('Working directory: ')
for i,file in enumerate(os.listdir(work_dir)):
if file.endswith(".txt"):
filename=os.path.abspath(file)
F = nx.read_gpickle(filename) #Loading graph G back into Python and calling it F
编辑
我收到此错误:ImportError: No module named copy_reg
,它指向 F=nx.read_gpickle(filename)
.
所在的行
我认为问题是我有一堆 txt
个文件,我正在尝试加载它们,就像它们是 gpickle
一样。如果我的看法是正确的,我如何在不改变图表特征的情况下将 .txt
文件转换为 .gpickle
?这样我就不用重新运行我的模拟了。
IOError 表明您引用的文件根本不存在,而不是加载不正确。您能否仔细检查您的脚本是否来自正确的文件夹,文本文件是否位于正确的位置等?
我也不熟悉 os.path.basename,但可能是您引用文件的方式导致了问题?
OP 的第一个错误(找不到文件)
尝试使用文件的 full/absolute 路径。确认 os.listdir(work_dir)
实际上 确实 指向正确的目录。
"I assume the problem is that I have a bunch of txt files and I am trying to load them as if they were gpickle" <-- 我不认为这是问题所在。错误发生在这个阶段之前。
运行 这是为了阐明正在发生的事情:
import os
work_dir=raw_input('Working directory: ')
if os.path.isdir(work_dir):
print "Directory exists:", work_dir
for i,f in enumerate(os.listdir(work_dir)):
if os.path.exists(f):
if os.path.isfile(f):
print "Found a file named:", f
else:
print "Found something else (dir) named:", f
else:
print "Invalid path within a valid work_dir:", f
else:
print "Work_dir does not exist:", work_dir
OP 的第二个错误(导入错误:没有名为 copy_reg 的模块)
这可能是由 pickle 文件的写入方式引起的。检查 this question 并查看是否使用 ẁb
(写入二进制)解决了它:
file = open("test.txt", 'wb')
thing = {'a': 1, 'b':2}
cPickle.dump(thing, file)
file.close()
我认为使用 rb
(读取二进制文件)进行阅读也不会造成任何伤害。
如果您要在 Linux 上加载写在 Windows 上的 pickle 文件,您可能需要执行另一个问题中提到的另一个技巧:
dos2unix originalPickle.file outputPickle.file
我想通了是什么错误。这些文件被写成 txt
with 'w'
using pickle.dump()
:
pickling=pickle.dump(G,open(original_dir2+'\pickling_test.txt','w'))
#G is the graph from networkx, and original_dir is the dir where the txt files were dumped
我试图用 nx.read_gpickle()
加载它们,这需要 .gpickle
个文件。
解决该问题的方法是使用 pickle.load(open(filename,"r"))
解封文件。然后解酸成功。
在处理同一图表 G
的多个不同实例后,我使用以下行将它们转储为 txt
文件 pickle
:
pickling=pickle.dump(G,open('pickled_G.txt','w')) #Example for one single graph
现在,为了进一步计算,我想通过执行以下操作将这些图加载回 networkx:
work_dir=raw_input('Working directory: ')
for i,file in enumerate(os.listdir(work_dir)):
if file.endswith(".txt"):
filename=os.path.abspath(file)
F = nx.read_gpickle(filename) #Loading graph G back into Python and calling it F
编辑
我收到此错误:ImportError: No module named copy_reg
,它指向 F=nx.read_gpickle(filename)
.
我认为问题是我有一堆 txt
个文件,我正在尝试加载它们,就像它们是 gpickle
一样。如果我的看法是正确的,我如何在不改变图表特征的情况下将 .txt
文件转换为 .gpickle
?这样我就不用重新运行我的模拟了。
IOError 表明您引用的文件根本不存在,而不是加载不正确。您能否仔细检查您的脚本是否来自正确的文件夹,文本文件是否位于正确的位置等?
我也不熟悉 os.path.basename,但可能是您引用文件的方式导致了问题?
OP 的第一个错误(找不到文件)
尝试使用文件的 full/absolute 路径。确认 os.listdir(work_dir)
实际上 确实 指向正确的目录。
"I assume the problem is that I have a bunch of txt files and I am trying to load them as if they were gpickle" <-- 我不认为这是问题所在。错误发生在这个阶段之前。
运行 这是为了阐明正在发生的事情:
import os
work_dir=raw_input('Working directory: ')
if os.path.isdir(work_dir):
print "Directory exists:", work_dir
for i,f in enumerate(os.listdir(work_dir)):
if os.path.exists(f):
if os.path.isfile(f):
print "Found a file named:", f
else:
print "Found something else (dir) named:", f
else:
print "Invalid path within a valid work_dir:", f
else:
print "Work_dir does not exist:", work_dir
OP 的第二个错误(导入错误:没有名为 copy_reg 的模块)
这可能是由 pickle 文件的写入方式引起的。检查 this question 并查看是否使用 ẁb
(写入二进制)解决了它:
file = open("test.txt", 'wb')
thing = {'a': 1, 'b':2}
cPickle.dump(thing, file)
file.close()
我认为使用 rb
(读取二进制文件)进行阅读也不会造成任何伤害。
如果您要在 Linux 上加载写在 Windows 上的 pickle 文件,您可能需要执行另一个问题中提到的另一个技巧:
dos2unix originalPickle.file outputPickle.file
我想通了是什么错误。这些文件被写成 txt
with 'w'
using pickle.dump()
:
pickling=pickle.dump(G,open(original_dir2+'\pickling_test.txt','w'))
#G is the graph from networkx, and original_dir is the dir where the txt files were dumped
我试图用 nx.read_gpickle()
加载它们,这需要 .gpickle
个文件。
解决该问题的方法是使用 pickle.load(open(filename,"r"))
解封文件。然后解酸成功。