[Tutor] Fwd: arrangement of datafile

Amrita Kumari amrita.g13 at gmail.com
Mon Jan 6 09:57:38 CET 2014


Hi Steven,

I tried this code:

import csv
with open('file.csv') as f:
     reader = csv.reader(f)
     for row in reader:
         print(row)
         row[0] = int(row[0])

up to this extent it is ok; it is ok it is giving the output as:

['1' , ' GLY' ,  'HA2=3.7850' ,  'HA3=3.9130' , ' ' , ' ' , ' ' , ' ']
[ '2' ,  'SER' ,  'H=8.8500' ,  'HA=4.3370' ,  'N=115.7570' , ' ' , ' ' , '
']
----------------------
-----------------------------------
but the command :

key, value = row[2].split('=', 1)
        value = float(value.strip())
        print(value)

is giving the value of row[2] element as

['1' , ' GLY' ,  'HA2=3.7850' ,  'HA3=3.9130' , ' ' , ' ' , ' ' , ' ']
3.7850
[ '2' ,  'SER' ,  'H=8.8500' ,  'HA=4.3370' ,  'N=115.7570' , ' ' , ' ' , '
']
8.8500
--------------------
------------------
so this is not what I want I want to print all the chemical shift value of
similar atom from each row at one time

like this:

1 HA2=3.7850
2 HA2=nil
3 HA2=nil
.....
............
..........
13 HA2=nil

similarly, for atom HA3:

1 HA3=3.9130
2 HA3=nil
3 HA3=nil
...........
............
............
13 HA3=nil  and so on.

so how to split each item into a key and a numeric value and then search
for similar atom and print its chemical shift value at one time along with
residue no..

Thanks,
Amrita





On Mon, Jan 6, 2014 at 6:44 AM, Steven D'Aprano <steve at pearwood.info> wrote:

> Hi Amrita,
>
> On Sun, Jan 05, 2014 at 10:01:16AM +0800, Amrita Kumari wrote:
>
> > I have saved my data in csv format now it is looking like this:
>
> If you have a file in CSV format, you should use the csv module to read
> the file.
>
> http://docs.python.org/3/library/csv.html
>
> If you're still using Python 2.x, you can read this instead:
>
> http://docs.python.org/2/library/csv.html
>
>
> I think that something like this should work for you:
>
> import csv
> with open('/path/to/your/file.csv') as f:
>     reader = csv.reader(f)
>     for row in reader:
>         print(row)
>
> Of course, you can process the rows, not just print them. Each row will
> be a list of strings. For example, you show the first row as this:
>
> > 2,ALA,C=178.255,CA=53.263,CB=18.411,,,,,,,,,,
>
> so the above code should print this for the first row:
>
> ['2', 'ALA', 'C=178.255', 'CA=53.263', 'CB=18.411', '', '', '',
> '', '', '', '', '', '']
>
>
> You can process each field as needed. For example, to convert the
> first field from a string to an int:
>
>         row[0] = int(row[0])
>
> To split the third item 'C=178.255' into a key ('C') and a numeric
> value:
>
>         key, value = row[2].split('=', 1)
>         value = float(value.strip())
>
>
>
> Now you know how to read CSV files. What do you want to do with the data
> in the file?
>
>
>
> --
> Steven
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140106/ad403eba/attachment.html>


More information about the Tutor mailing list