Skip to content

Instantly share code, notes, and snippets.

@longjie
Last active April 3, 2016 07:20
Show Gist options
  • Save longjie/714561d9d3bfcf70d7695e37dd204d19 to your computer and use it in GitHub Desktop.
Save longjie/714561d9d3bfcf70d7695e37dd204d19 to your computer and use it in GitHub Desktop.
スペース区切りのファイルに対するデータの読み書き
#!/usr/bin/env python
from __future__ import print_function
data = [['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']]
# write data
with open('data.txt', 'w') as file:
[file.write(' '.join(x)+'\n') for x in data]
# read data
with open('data.txt', 'r') as file:
data = [x.split() for x in file]
print(data)
@longjie
Copy link
Author

longjie commented Apr 3, 2016

list(map(lambda x: print(x, file=file), data))

とすると評価されるがこれだとリスト内包表記の方がいい。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment