[Tutor] Fwd: arrangement of datafile

Steven D'Aprano steve at pearwood.info
Tue Jan 7 12:30:05 CET 2014


On Mon, Jan 06, 2014 at 04:57:38PM +0800, Amrita Kumari wrote:
> 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' , ' ' , ' ' , '
> ']

It looks like you are re-typing the output into your email. It is much 
better if you copy and paste it so that we can see exactly what happens.


> 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 far, the code is doing exactly what you told it to do. Take the third 
column (index 2), and split on the equals sign. Convert the part on the 
right of the equals sign to a float, and print the float.


> 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

Okay, then do so. You'll have to write some code to do this.


> like this:
> 
> 1 HA2=3.7850
> 2 HA2=nil
> 3 HA2=nil

Where do these values come from? 



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

I've already shown you how to split an item into a key and numeric 
value. Here it is again:

key, value = item.split('=', 1)
value = float(value)


> and then search
> for similar atom and print its chemical shift value at one time along with
> residue no..

I don't know what a chemical shift value and residue number are. 
Remember, we are Python programmers, not chemists or biochemists or 
whatever your field is. We don't know how to solve your problem, but if 
you describe in simple English terms how you would solve that problem, 
we can probably help you turn it into Python code.

Start with one row, containing this data:

'2', 'SER', 'H=8.8500', 'HA=4.3370', 'N=115.7570', '', '', ''

There are eight columns. What do those columns represent? In simple 
English terms, what would you like to do with those columns? Tell us 
step by step, as if you were explaining to a small child or a computer.



-- 
Steven


More information about the Tutor mailing list