[Tutor] Translating R Code to Python-- reading in csv files, writing out to csv files

Benjamin G benjaming286 at gmail.com
Sat May 19 23:07:04 CEST 2012


Thanks, Joel.  Here is a simple example of the input data (top row is
column headers in the input file)
Patient ID                             Sample Type  Survival Time(months)
unique_patient_ID1                Tumor           12
unique_patient_ID2                Normal          5
unique_patient_ID3                Normal          60
unique_patient_ID4                Tumor           2
unique_patient_ID5                Tumor           13

Since we are handling many different input files, we standardized the
column names and values.  For example, we want the output file to have the
following column headers instead of the three above: alt_sample_name,
sample_type and days_to_death

The output file should look like this:
alt_sampleName  sample_type  days_to_death
1                         tumor            360
2                         normal          150
3                         normal          1800
4                         tumor            60
5                         tumor            390

Thanks again,

Benjamin



On Sat, May 19, 2012 at 2:18 PM, Joel Goldstick <joel.goldstick at gmail.com>wrote:

> On Sat, May 19, 2012 at 1:58 PM,  <eire1130 at gmail.com> wrote:
> > I confese I haven't read your entire email, but have you looked at pyR?
> I believe it is a wrapper for python, giiving you access to r.
> >
> >
> > Sent from my Verizon Wireless BlackBerry
> >
> > -----Original Message-----
> > From: Benjamin G <benjaming286 at gmail.com>
> > Sender: tutor-bounces+eire1130=gmail.com at python.org
> > Date: Sat, 19 May 2012 13:46:42
> > To: <tutor at python.org>
> > Subject: [Tutor] Translating R Code to Python-- reading in csv files,
> >  writing out to csv files
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
>
> I am not sure I understand what you want to do.  Could you show a few
> lines of your input data and then show how you want it to look once it
> is processed?  The csv module is pretty easy to get going.
>
> If your goal is to learn python I would check the documentation or
> find a csv tutorial http://docs.python.org/library/csv.html
>
> The reader will either write to python list or dict.  If you use the
> list, you access each item with my_list[n] where n is the position
> (starting with 0).  If you need to change the data, you can do that to
> the list items.  If you need to output in a different order, or just
> some fields try this:
>
> in_date:  [1, 2, 3] --> out_data [2,1,3] would code like this:
> out_data[]
> out_data.append[1]
> out_data.append[0]
> out_data.append[2]
>
> Then use the csv writer to write out_data to your file
>
> I don't know R, but most python people will tell you its best to learn
> the 'python way', and not try to 'translate' from another language.
>
> --
> Joel Goldstick
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120519/692eab91/attachment.html>


More information about the Tutor mailing list