Python - creating a table

I'm a python user at an early stage.

I have two sets of temperature data in a particular place from 1850 to 2010 with one temperature value for each month for the entire period. I'm trying to create a table with these values ​​in the following format. T - my data.

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

Sorry, I can not publish a picture of the table, as I need to. I can not publish the photo. and I can not specify the form of my table that I need. so I send a link to a different table designs. his other set of data. but I need to have two lines compared with a year. one for my dannyh1 and one for my data 2.

Now I have one full series of data ranging from 1850 to 2010. I want to rewrite the two datasets in the above format as a table. I cut out the data Data1 and 2 data for each year. I know that it is easy accessible work through an office suite, but I know that this is not the way to programming. please help me with this.

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)] , t, t, t, t, t, t, ............................. 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)] , t, t, t, t, t, t, ............................. 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)] data for the entire period from 1850-2010 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)] + 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 a value for dannyh1 and for data 2, cut each year. data1_yearly [0] gives me the value of the data for the year 1850 and subsequent indexing will give me the data for the whole period.

So here begins my problem. How can I write this data in the form of a table in the above format. I am completely unfamiliar with the language, so please do not consider this request stupid and kindly ask for me.

+4
source share
2 answers

I would recommend you look at the string patterns

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' likes $ what') >>> 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' , what = 'kung pao') >>> 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' tim') >>> 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' what'). Substitute (d) >>> 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' what'). Safe_substitute (d) >>> 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 template string target format and then put the data in the dictionary, as described above, the conversion should be easy.

That is, if I correctly interpret your question, ie, you want to print a beautiful table in stdout ...

+2
source

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

 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]])) 'year', 'data', 'Jan', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', ' 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]])) NOV ',' DEZ ']) 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]])) )): 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]])) \ t% s \ n \ t% s \ t% s"% ((theYearOffset + theYearCounter), 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]])) .join ([ "%. 2f"% theValue for theValue in data1_yearly [theYearCounter]]), 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, but it will do the job. The columns are separated by tabs, and floating point number rounded up to two digits.

Here are the results 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] 
+1
source

All Articles