开发者

Python- creating a table

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 dat开发者_开发知识库a2 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.


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


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:

Python- creating a table

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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜