Python-在网格中显示Excel数据库

Python-在网格中显示Excel数据库,python,excel,python-2.7,pygame,grid-layout,Python,Excel,Python 2.7,Pygame,Grid Layout,我是一名考古学家,正在处理一条挖掘沟槽的结果,该沟槽以6x6的方格排列,每个方格为5米x 5米。 我想在网格上绘制挖掘中发现的东西的分布图 我想在6x6 Python/Pygame网格中显示一些Excel数据库值。 有人能告诉我怎么做吗? 我想我知道如何让Python在*.csv中读取Excel文档,但我无法在网格中显示它 编辑有关数据的信息 可以在链接中找到数据片段。我想在网格中显示“分类”列中的项目。 例如, 每个正方形中的“制备薄片”数量(以数字或颜色/形状表示),其中每个正方形对应于S

我是一名考古学家,正在处理一条挖掘沟槽的结果,该沟槽以6x6的方格排列,每个方格为5米x 5米。
我想在网格上绘制挖掘中发现的东西的分布图

我想在6x6 Python/Pygame网格中显示一些Excel数据库值。
有人能告诉我怎么做吗?
我想我知道如何让Python在
*.csv
中读取Excel文档,但我无法在网格中显示它

编辑有关数据的信息
可以在链接中找到数据片段。我想在网格中显示“分类”列中的项目。

例如,
每个正方形中的“制备薄片”数量(以数字或颜色/形状表示),其中每个正方形对应于S列中的gridsquare数字。数据库变得相当大。
所以,
理想情况下,我希望程序能够读取数据库并更新square,而无需不断更改程序

代码如下

<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

因为您没有告诉我们您使用的是哪种数据,
我已经取了整数,但任何字符串都可以

以下是如何显示
包含字符串的网格(本例中为数字)在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
这导致


更新
嗯,汤姆,
对于csv文件,您可以将此功能添加到脚本中,并使用它代替
text\u 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\u display\u csv

改变

text = [str(i) for i in range(36)] # raw data!
text_display(text,screen) # text is displayed
pygame.display.update() # screen updated


这样代码就可以工作了。(如果需要,请更改间距和偏移量)

您能给出一些示例数据和结果的大致图像吗?抱歉,我没有说得那么清楚。我将在描述中添加更多的内容。显然,我不允许添加图片,这很烦人。很抱歉。样本数据分类-预操作薄片;蓝色;铜绿-白色;背景-3667;网格Suare-5@TomLawrence在免费的图像托管服务上托管图像,并将链接粘贴到问题中的图像。谢谢!它真的很有用!!!因此,对于要显示excel数据的程序,我会将“disp(data[x+(y*6)],(64*x+10,64*y+10),surface)”更改为包含“c:row[2]中的行的c=csv.reader(open(“lym12 based data1.csv”)”的内容。TomLawrence看到更新,它回答了这个问题。此外,在注释中对
code
使用“not”,或在问题中对变量名使用“not”。(这是旁边的钥匙)谢谢你的帮助,但它不太管用。。错误表示未定义文本显示使用
text\u display\u csv
text\u display
!!是的。我会再看一遍再核对一下
filename = 'the .csv file'
text_display_csv(filename,screen) # text is displayed
pygame.display.update() # screen updated