Python在网格中显示excel数据库第2部分

2024-04-25 17:53:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是创建一个类似于http://i49.tinypic.com/f39hxv.png的网格,其中每个数字表示在每个正方形中找到的“准备工作薄片”的数量(参见下面的数据片段)

目前的代码如下:

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",10) # font initialisation
screen = pygame.display.set_mode((1200,1200))


filename = 'the .csv file'
text_display_csv(filename,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

我希望网格看起来有点像这样(但我的问题是将数据库中的数据放入此数组):

^{pr2}$

数据如下:

http://tinypic.com/view.php?pic=2rdgn02&s=6

所以总结一下。我希望不同的'分类'(见数据)显示在网格中,就像图片在开始。在

谢谢你的帮助

汤姆


Tags: csvthe数据inimport网格fordisplay
1条回答
网友
1楼 · 发布于 2024-04-25 17:53:14

这里有一些代码,
这是您的代码,最多10个字符(注释和删除除外),但会导致大的更改。在

import pygame,csv

def disp(phrase,loc,screen): 
    s = font.render(phrase, True, (255,255,255))
    screen.blit(s, loc)

# not needed

def text_display_csv(filename,surface):
    '''Display .csv file contents on surface'''
    f = csv.reader(open(filename))

    for row in f:
        y = f.line_num                
        for x,item in enumerate(row):
            disp(item, (60*x+10,12*y-2), surface) # changed y distance

pygame.init()
pygame.font.init()
font = pygame.font.SysFont("Courier",10) # Changed font size
screen = pygame.display.set_mode((640,480)) # changed window size

filename = "Flint catalogue v1.7 4 Feb 13 2013.csv"
text_display_csv(filename,screen) 
pygame.display.update()

# 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

更新1 好吧,我明白了。 所以,你想用P来显示网格号的数据。。。在

这也是密码。把text_display_csv改成这个

^{pr2}$

这里发生了什么:

  1. 加载.csv
  2. 如果一行至少有S列的项目,
    • 如果S处的值是一个数字,则获取它的整数值。
      然后将信息以S : P的形式添加到字典中,其中
      • S是整数
      • PP处的字符串/值。在
    • 否则,打印一条消息,然后转到下一行(如果有)
  3. 在各自的位置显示数据。在

现在唯一的问题是数字必须从零开始。
为了克服这个问题,只需添加

loc += 1

以前

y = loc//width

你的网格线可以用pygame.draw.line绘制,你可以搞清楚。在

相关问题 更多 >