Created
March 15, 2014 07:17
-
-
Save pixyj/9562964 to your computer and use it in GitHub Desktop.
Write a Python sequence into a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def write(file_path, sequence, line_format): | |
""" | |
Example Usage: Save squares of numbers from 1 to 100 in a csv file | |
squares = ( (i, i * i) for i in range(1, 101)) | |
write("squares.txt", squares, "{},{}") | |
""" | |
lines = (line_format.format(*line_variables) for line_variables in sequence) | |
s = "\n".join(lines) | |
with open(file_path, "w") as f: | |
f.write(s) | |
return s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment