[Tutor] Fwd: arrangement of datafile
Keith Winston
keithwins at gmail.com
Mon Jan 6 17:26:51 CET 2014
Amrita, it doesn't seem like the code you are providing is the code you are
running. I wonder if you are running it all at the Python command line or
something, and have to type it in every time? You should put it in a file,
and save & run that file, and then cut and paste it directly into your
emails, so we can see what you're actually running. There are a number of
small things in the code you've posted that wouldn't run, I think...
Keith
On Mon, Jan 6, 2014 at 3:57 AM, Amrita Kumari <amrita.g13 at gmail.com> 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' , ' ' , ' ' ,
> ' ']
> ----------------------
> -----------------------------------
> 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
>>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
--
Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140106/c2a78956/attachment-0001.html>
More information about the Tutor
mailing list