Python - 在网格中显示Excel数据库
我是一名考古学家,正在处理一个挖掘坑的结果,这个坑是一个6x6的方格网格,每个方格的大小是5米 x 5米。
我想在这个网格上绘制出挖掘中发现的物品的分布情况。
我想在一个6x6的Python/Pygame网格中显示一些Excel数据库中的值。
请问有人能告诉我我该怎么做吗?
我觉得我知道怎么让Python读取一个*.csv
格式的Excel文档,但我不知道怎么把它显示在网格里。
关于数据的补充说明
数据的一个片段可以在这个链接找到。我想在网格中显示“分类”这一列中的项目。
http://i48.tinypic.com/2rdgn02.jpg
举个例子,
我想在每个方格中显示“准备片”的数量,可以用数字或者颜色/形状来表示,每个方格对应于S列中的网格编号。数据库的数据量越来越大。
所以,
理想情况下,我希望程序能自动读取数据库并更新方格,而不需要我不断地修改程序。
现在的代码如下。
<import csv
import os
import pygame
# the functions for display
def disp(phrase,loc,screen): # function to display phrase at loc on surface.
s = font.render(phrase, True, (255,255,255))
screen.blit(s, loc) #
def text_display_csv(filename,surface):
'''Display .csv file contents on surface'''
f = csv.reader(open("Flint catalogue v1.7 4 Feb 13 2013.csv")) # open the csv file in one line!
for row in f:
y = f.line_num # assign y as Row no.
for x,item in enumerate(row): # Get column number(x) and item
disp(item, (64*x+10,64*y+10), surface) # display item
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("Courier",36) # font initialisation
screen = pygame.display.set_mode((800,600))
# the part you want to see
text = [str(i) for i in range(36)] # raw data!
text_display_csv(text,screen) # text is displayed
pygame.display.update() # screen updated
# Main loop, does nothing! :)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
break
if not running:
break
如果可能的话,我希望结果能以类似下面图片的方式显示,每个数字代表方格中“准备片”的数量。 http://i49.tinypic.com/f39hxv.png
我非常感谢任何帮助,如果有不清楚的地方,请告诉我。
汤姆
1 个回答
0
因为你没有告诉我们你使用的数据是什么类型,
我这里用的是整数,不过任何字符串都可以。
下面是如何在 Pygame 中显示
一个包含字符串的网格(在这个例子中是数字):
import pygame
# the functions for display
def disp(phrase,loc,screen): # function to display phrase at loc on surface.
s = font.render(phrase, True, (255,255,255))
screen.blit(s, loc) #
def text_display(data,surface):
'''display your data(list/tuple),containing strings on surface'''
for x in range(6): # your data is in 6 X 6 grid
for y in range(6):
disp( data[x+(y*6)], (64*x+10,64*y+10), surface)
# 64 is the distance between 2 strings and 10 is the offset
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("Courier",36) # font initialisation
screen = pygame.display.set_mode((400,400))
text = [str(i) for i in range(36)] # raw data!
text_display(text,screen) # text is displayed
pygame.display.update() # screen updated
# Main loop, does nothing! :)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
break
if not running:
break
这样会得到
更新
好吧,Tom,
对于 csv 文件,你可以把这个函数加到脚本里,替代 text_display
def text_display_csv(filename,surface):
'''Display .csv file contents on surface'''
f = csv.reader(open(filename)) # open the csv file in one line!
for row in f:
y = f.line_num # assign y as Row no.
for x,item in enumerate(row): # Get column number(x) and item
disp(item, (64*x+10,64*y+10), surface) # display item
我还没测试过,所以请告诉我它是否有效。
更新 2
在第一个代码块中,添加 text_display_csv
,然后
把
text = [str(i) for i in range(36)] # raw data!
text_display(text,screen) # text is displayed
pygame.display.update() # screen updated
改成
filename = 'the .csv file'
text_display_csv(filename,screen) # text is displayed
pygame.display.update() # screen updated
这样代码才能正常工作。(如果需要,可以调整间距和偏移量)