【问题标题】:Python - Display Excel database in a gridPython - 在网格中显示 Excel 数据库
【发布时间】:2013-02-05 08:59:21
【问题描述】:

我是一名考古学家,正在处理以 6x6 正方形网格布置的挖掘沟的结果 - 每个正方形为 5 m x 5 m。
我想在网格上绘制挖掘中发现的发现的分布。

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

修改数据
可以在链接中找到数据片段。我想显示网格中“分类”列中的项目。 http://i48.tinypic.com/2rdgn02.jpg

例如,
每个正方形中“准备薄片”的数量,可以是数字,也可以是颜色/形状,其中每个正方形对应于 S 列中的 gridsquare 数。数据库变得非常大。
所以,
理想情况下,我希望程序能够读取数据库并更新方块,而无需我不断更改程序。

代码如下。

<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

我非常感谢任何帮助,如果这没有意义,请说出来

汤姆

【问题讨论】:

  • 您能否提供一些示例数据以及结果应该是什么样子的粗略图像?
  • 对不起,我没有像我应该做的那样清楚。我将在描述中添加更多内容
  • 显然我不允许添加令人讨厌的图片。对此感到抱歉。
  • 样本数据分类 - 制备薄片;颜色-蓝色;铜绿 - 白色;上下文 - 3667;网格苏亚雷 - 5;
  • @TomLawrence 在免费图片托管服务上托管图片并将链接粘贴到问题中的图片。

标签: python excel python-2.7 pygame grid-layout


【解决方案1】:

由于您尚未告诉我们您使用的是哪种数据,
我取了整数,但任何字符串都可以。

以下是如何在 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_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

这样代码才能工作。 (根据需要更改间距和偏移量)

【讨论】:

  • 谢谢!真的好用!!!因此,为了让程序显示 excel 数据,我会将 'disp(data[x+(y*6)], (64*x+10,64*y+10), surface)' 更改为包含 'c = csv .reader(open("lym12 bedrock data1.csv")) for row in c: row[2]' ?????????
  • @TomLawrence 查看更新,它回答了这个问题。此外,对于 cmets 中的 code 或问题中的变量名,请使用 `not'。 (是 1 旁边的键)
  • 感谢您的帮助,但它并不完全有效.. 错误说 text_display 未定义
  • 使用text_display_csv 而不是text_display !!
  • 是的,我做到了。我会再看一遍仔细检查
猜你喜欢
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 2014-12-22
  • 2022-01-09
相关资源
最近更新 更多