Skip to content

Instantly share code, notes, and snippets.

@arc279
Last active June 23, 2017 06:59
Show Gist options
  • Save arc279/afb7463fe03fc08981d6090f6a079b6e to your computer and use it in GitHub Desktop.
Save arc279/afb7463fe03fc08981d6090f6a079b6e to your computer and use it in GitHub Desktop.
pickleに追記と読み出し
import pickle
def dump_seq(r, of):
with open(of, "wb") as f:
pickle.dump(next(r), f)
try:
while True:
with open(of, "ab") as f:
pickle.dump(next(r), f)
except StopIteration as e:
pass
def load_seq(fp):
try:
while True:
yield pickle.load(f)
except EOFError as e:
raise StopIteration(e)
# https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv
r = pd.read_csv(open("iris.csv"), chunksize=20)
dump_seq(r, "o")
with open("o", "rb") as f:
for i, x in enumerate(load_seq(f)):
print(i, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment