Skip to content

Instantly share code, notes, and snippets.

@pixyj
Created March 15, 2014 07:17
Show Gist options
  • Save pixyj/9562964 to your computer and use it in GitHub Desktop.
Save pixyj/9562964 to your computer and use it in GitHub Desktop.
Write a Python sequence into a file
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