Skip to content

Instantly share code, notes, and snippets.

@rhiever
Created November 16, 2013 04:46
Show Gist options
  • Save rhiever/7496047 to your computer and use it in GitHub Desktop.
Save rhiever/7496047 to your computer and use it in GitHub Desktop.
Saving data to a text file in Python
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Saving data to a file in Python"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from numpy import arange\n",
"\n",
"fitness_list = arange(1, 50, 2)\n",
"trait1_list = arange(0, 250, 10)\n",
"trait2_list = arange(150, 0, -6)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"replicate = 5\n",
"\n",
"with open(\"output_data\" + str(replicate) + \".csv\", \"w\") as out_file:\n",
" for i in range(len(fitness_list)):\n",
" out_string = \"\"\n",
" out_string += str(fitness_list[i])\n",
" out_string += \",\" + str(trait1_list[i])\n",
" out_string += \",\" + str(trait2_list[i])\n",
" out_string += \"\\n\"\n",
" out_file.write(out_string)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!ls output_*"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"output_data.csv output_data5.csv\r\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment