4

I am a python user in the very early stage.

I have two data set of temperature over a specific location from the year 1850 to 2010 with one value of temperature for each month for this entire period. I am trying to create a table with these values in the below given format. T is my data.

year data JAn FEB MAR APR MAY JUN JUL AUG SEP .......DEC.
1850 data1 t   t   t   t   t   t   t   t   t          t.
     data2 t   t   t   t   t   t   t   t   t          t.
'.
'.
'.
2010 data1 t   t   t   t   t   t   t   t  t           t.

I am sorry i cannnot post a picture of the table how i need. I am not allowed to post a picture. and i am not able to specify the shape of my table i need. so am posting a link of another sample table. its another data set. but i need to have two rows against the year. one for my data1 and one for my data 2.

Now what i have is the complete one series of data ranging from 1850 to 2010. I want to rewrite the two data sets in the above given format as a table. from the data i have sliced data1 and data 2 for each year. I know it is an easily accomplish able job through an office package, but i know that is not the way of programing. please some one help me in doing it.

Here is what i have now.

data1 = [t, t, t, t, t, t, t, t, t,..............................t]
data2 = [t, t, t, t, t, t, t, t, t,..............................t]

#This data1 and data2 is the list of data for the entire period from 1850-2010
#I sliced this data as
n = len(data1)
data1_yearly = [data1[i:i+12] for i in xrange(0,n,12)]
data2_yearly = [data2[i:i+12] for i in xrange(0,n,12)]

Now I have the values for both data1 and data2 sliced for each year. data1_yearly[0] gives me the value of the data for the year 1850 and further indexing will give me the data for all the period.

So from here starts my problem. How can I write this data as a table in the format I specified above. I am purely new to this language and so please don't consider this request a foolish one and kindly hlep me.

Rajeev
  • 41
  • 1
  • 1
  • 4
  • 1
    You need to be more specific about what you mean by "a table." Are you talking about a text file that looks like your first snippet, or do you mean entries in a relational database, or what? – jhocking May 25 '11 at 19:28
  • I'm not sure how helpful it is, but you should have a look at `numpy`. – Daenyth May 25 '11 at 19:28
  • I am sorry if am not giving the description correctly. Printing a text file or just giving a list in that shape is ok for me. I am beginer to numpy and python, and i tried lot of wrong things to add the data in the desired rows and columns. – Rajeev May 25 '11 at 19:29
  • Please be more precise: is the problem that you want to parse data in a given input format into Python lists, that you want to render Python lists into an output format, or both? Also, if you are trying to convert a particular file, you'll get better answers if you provide a link to your source for the file. – Sasha May 25 '11 at 19:29
  • Saha now i have on single series of data set ranging from 1850 january to 2010 december. I want to rewrite that data, as rows and columns and in rows i should have the data for an year and in columns i shoul have the month. I will edit my question and will add the data file i have now. – Rajeev May 25 '11 at 19:36
  • To clarify: (A) you don't need help reading the file, only producing output. (B) when the program runs, you have two Python lists called data1 and data2. each contains monthly data for 161 years, being 1850-2010 - thus both lists have a length of 1,932. (C) you want to produce text output which looks similar to your first table. thus, you want 161 pairs of rows (total 322 rows, alternating data1 then data2), with each row containing the following fields: the year, the name of the data series (data1 or data2), and one number for each month of the year (January through December). is that right? – Sasha May 25 '11 at 20:38

2 Answers2

2

I would recommend that you have a look at string templates

Example:

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
[...]
ValueError: Invalid placeholder in string: line 1, col 10
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
[...]
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

If you create a string template of the target format and then place the data in a dictionary as described above, the conversion should be easy.

That is, if I interpreted your question correctly, i.e. you want to print a nice table to stdout...

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
1

To print the above data in a table I would suggest a simple loop with some string formating:

print "\t".join(['year', 'data', 'Jan', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEZ'])
theYearOffset = 1850
for theYearCounter in range(len(data1_yearly)):
    print "%s\t%s\t%s\n\t%s\t%s" % ((theYearOffset + theYearCounter), 
        'data1', "\t".join(["%.2f" % theValue for theValue in data1_yearly[theYearCounter]]), 
        'data2', "\t".join(["%.2f" % theValue for theValue in data2_yearly[theYearCounter]]))

This is not the most beautiful code possible but it will do the job. The columns are separated with tabulators and floating point numbers are roundet to 2 digits.

Here is the output for some stupid test data:

output

Test data:

data1 = [1.1233,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11]
data2 = [8,9,10,11,12,13,14,15,1,2,4,8,9,10,11,12,13,14,15,1,2,4,8,9,10,11,12,13,14,15,1,2,4,8,9,10,11,12,13,14,15,1,2,4]
spassig
  • 1,041
  • 1
  • 7
  • 5
  • Welcome to the python world. Python doas a lot syntactic suggar (you allready used list comprehension). You may take a look at http://stackoverflow.com/questions/101268 for some nice features of this language – spassig May 26 '11 at 07:19
  • Ya thanks for the information. i am really a beginner. will catch up surely. – Rajeev May 26 '11 at 10:00