Ask Your Question
0

Manipulate a csv file using ipython notebook

asked 2015-05-06 08:19:31 +0100

sydney gravatar image

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      6
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-05-06 11:43:22 +0100

tmonteil gravatar image

Actually, this is not related to ipython notebook but about Python, you can have a look at the csv module

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.

edit flag offensive delete link more

Comments

Thank 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?

sydney gravatar imagesydney ( 2015-05-06 18:15:27 +0100 )edit

What 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.

tmonteil gravatar imagetmonteil ( 2015-05-06 20:41:59 +0100 )edit

To 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
6
tmonteil gravatar imagetmonteil ( 2015-05-06 20:42:21 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-05-06 08:19:31 +0100

Seen: 3,347 times

Last updated: May 06 '15