Python - 在网格中显示Excel数据库 - 第2部分
我的目标是创建一个网格,样子像这个 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
我希望这个网格看起来像这样(但我遇到的问题是如何把数据库里的数据放到这个数组里):
import csv
import os
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
# This sets the width and height of each grid location
width=20
height=20
# This sets the margin between each cell
margin=5
# Create a 2 dimensional array. A two dimesional
# array is simply a list of lists.
grid=[]
for row in range(10):
# Add an empty array that will hold each cell
# in this row
grid.append([])
for column in range(10):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
grid[1][6] = 2
# Initialize pygame
pygame.init()
# Set the height and width of the screen
size=[255,255]
screen=pygame.display.set_mode(size)
# -------- Main Program Loop -----------
while done==False:
# Set the screen background
screen.fill(black)
# Draw the grid
for row in range(10):
for column in range(10):
color = white
if row[4]in c == 1:
color = green
pygame.draw.rect(screen,color,[(margin+width)*column+margin, (margin+height)*row+margin,width,height])
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.quit()
数据如下:
http://tinypic.com/view.php?pic=2rdgn02&s=6
总之,我希望在网格中显示不同的“分类”(见数据),就像一开始的图片那样。
谢谢你的帮助
汤姆
1 个回答
0
这里有一些代码,
这段代码和你的代码差不多,只改了最多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
好的,我明白了。
所以,你想在没有 S 的网格中显示 P 的数据……
这也是相关的代码。只需把 text_display_csv
改成这个:
def text_display_csv(filename,surface):
'''Display .csv file contents on surface, after processing'''
f = csv.reader(open(filename))
# The Processing
data = {}
for row in f:
y = f.line_num
if len(row) >= 19:
# See if S contains a number,
# if No: Print the msg
# else: add to dict, in form of S:P
try:
val = int(row[18]) # S is the 19th column
except ValueError:
print "Skipped row no",y,"as column S doesn't contain number"
continue
else: # if a number, add to the dict
data[val] = row[15] # P is the 16th column
print "File Loaded"
# the display part
width = 6
for loc in data:
item = data[loc] # get item (string to display)
y = loc//width # column
x = loc % width # row
disp(item, (60*x +10,30*y +10), surface)
这里发生了什么:
- 加载
.csv
文件 - 如果某一行的项目至少到达了
S
列,
- 如果
S
的值是一个数字,就取它的整数值。
然后把信息以S : P
的形式添加到一个字典中,其中
S
是整数P
是P
列的字符串/值。
- 否则打印一条消息,然后继续处理下一行(如果有的话)
- 如果
- 在各自的位置显示数据。
现在唯一的问题是数字必须以零开头。
要解决这个问题,只需添加
loc += 1
在
y = loc//width
你的网格线可以使用 pygame.draw.line
来绘制,你可以自己搞定这个。