ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 06 May 2015 13:42:21 -0500Manipulate a csv file using ipython notebookhttp://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/ I have a csv file which includes:
Branch Ra L C Rb
1 1 10 1 1
2 13 100 15 6
How can I get an output from this csv file which will look like`
Branch1:
Ra1 ndT nd1b 1
L1 nd1b nd1c 10
C1 nd1c 0 1
Rb1 nd1c 0 1
Branch2:
Ra2 ndT nd2b 13
L2 nd2b nd2c 100
C2 nd2c 0 15
Rb2 nd2c 0 6Wed, 06 May 2015 01:19:31 -0500http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/Answer by tmonteil for <p>I have a csv file which includes:</p>
<pre><code>Branch Ra L C Rb
1 1 10 1 1
2 13 100 15 6
</code></pre>
<p>How can I get an output from this csv file which will look like`</p>
<pre><code>Branch1:
Ra1 ndT nd1b 1
L1 nd1b nd1c 10
C1 nd1c 0 1
Rb1 nd1c 0 1
Branch2:
Ra2 ndT nd2b 13
L2 nd2b nd2c 100
C2 nd2c 0 15
Rb2 nd2c 0 6
</code></pre>
http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?answer=26734#post-id-26734Actually, this is not related to ipython notebook but about Python, you can have a look at the [csv module](https://docs.python.org/2/library/csv.html)
To import `csv`:
sage: import csv
In your case, the delimiter is not a coma, but a sequence of spaces, so you have to tell `csv` that the delimiter is a space (you can only use a single character for the delimiter), and that the other spaces should be skipped.
Now, you can load your csv file and loop to each of its rows, and each row will be a list of columns:
sage: with open('/tmp/plop.csv', 'rb') as my_file:
....: reader = csv.reader(my_file, delimiter=' ', skipinitialspace=True)
....: for row in reader:
....: print row
['Branch', 'Ra', 'L', 'C', 'Rb']
['1', '1', '10', '1', '1']
['2', '13', '100', '15', '6']
You should be able to conclude from this.
Wed, 06 May 2015 04:43:22 -0500http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?answer=26734#post-id-26734Comment by tmonteil for <p>Actually, this is not related to ipython notebook but about Python, you can have a look at the <a href="https://docs.python.org/2/library/csv.html">csv module</a></p>
<p>To import <code>csv</code>:</p>
<pre><code>sage: import csv
</code></pre>
<p>In your case, the delimiter is not a coma, but a sequence of spaces, so you have to tell <code>csv</code> that the delimiter is a space (you can only use a single character for the delimiter), and that the other spaces should be skipped.</p>
<p>Now, you can load your csv file and loop to each of its rows, and each row will be a list of columns:</p>
<pre><code>sage: with open('/tmp/plop.csv', 'rb') as my_file:
....: reader = csv.reader(my_file, delimiter=' ', skipinitialspace=True)
....: for row in reader:
....: print row
['Branch', 'Ra', 'L', 'C', 'Rb']
['1', '1', '10', '1', '1']
['2', '13', '100', '15', '6']
</code></pre>
<p>You should be able to conclude from this.</p>
http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?comment=26746#post-id-26746What you did will output the follwing:
['Branch Ra L C Rb']
[' 1 1 10 1 1']
[' 2 13 100 15 6']
So, for each row, you get a list with a single element that is a big string. So you do not use the fact that your file is structured with rows and columns. What you need is to split each row according to the space, which is a delimiter.Wed, 06 May 2015 13:41:59 -0500http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?comment=26746#post-id-26746Comment by tmonteil for <p>Actually, this is not related to ipython notebook but about Python, you can have a look at the <a href="https://docs.python.org/2/library/csv.html">csv module</a></p>
<p>To import <code>csv</code>:</p>
<pre><code>sage: import csv
</code></pre>
<p>In your case, the delimiter is not a coma, but a sequence of spaces, so you have to tell <code>csv</code> that the delimiter is a space (you can only use a single character for the delimiter), and that the other spaces should be skipped.</p>
<p>Now, you can load your csv file and loop to each of its rows, and each row will be a list of columns:</p>
<pre><code>sage: with open('/tmp/plop.csv', 'rb') as my_file:
....: reader = csv.reader(my_file, delimiter=' ', skipinitialspace=True)
....: for row in reader:
....: print row
['Branch', 'Ra', 'L', 'C', 'Rb']
['1', '1', '10', '1', '1']
['2', '13', '100', '15', '6']
</code></pre>
<p>You should be able to conclude from this.</p>
http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?comment=26747#post-id-26747To be more precise, with the proposed solution you can do something like:
sage: with open('/tmp/plop.csv', 'rb') as my_file:
....: reader = csv.reader(my_file, delimiter=' ', skipinitialspace=True)
....: for row in reader:
....: print row
....: for column in row:
....: print column
....:
Branch
Ra
L
C
Rb
1
1
10
1
1
2
13
100
15
6Wed, 06 May 2015 13:42:21 -0500http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?comment=26747#post-id-26747Comment by sydney for <p>Actually, this is not related to ipython notebook but about Python, you can have a look at the <a href="https://docs.python.org/2/library/csv.html">csv module</a></p>
<p>To import <code>csv</code>:</p>
<pre><code>sage: import csv
</code></pre>
<p>In your case, the delimiter is not a coma, but a sequence of spaces, so you have to tell <code>csv</code> that the delimiter is a space (you can only use a single character for the delimiter), and that the other spaces should be skipped.</p>
<p>Now, you can load your csv file and loop to each of its rows, and each row will be a list of columns:</p>
<pre><code>sage: with open('/tmp/plop.csv', 'rb') as my_file:
....: reader = csv.reader(my_file, delimiter=' ', skipinitialspace=True)
....: for row in reader:
....: print row
['Branch', 'Ra', 'L', 'C', 'Rb']
['1', '1', '10', '1', '1']
['2', '13', '100', '15', '6']
</code></pre>
<p>You should be able to conclude from this.</p>
http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?comment=26740#post-id-26740Thank you so much for your reply. I used this code:
import csv
ifile = open('circuit.csv')
read = csv.reader(ifile)
for row in read :
print (row)
I got the same output as you got. But after that I stuck to run the for loop. Is it a problem that I did not mention about delimiter?Wed, 06 May 2015 11:15:27 -0500http://ask.sagemath.org/question/26731/manipulate-a-csv-file-using-ipython-notebook/?comment=26740#post-id-26740