如何将行从CSV文件存储到Python中并使用HTML打印数据 [英] How to Store Rows from CSV File into Python and Print Data with HTML
问题描述
基本上我的问题是这样的:我有一个CSV excel文件,其中有一个关于Southpark字符的信息,我和我有一个HTML模板,我要做的是将数据按行(存储在列表中)用于每个字符,并使用给出的HTML模板实现了数据创建5个具有字符姓氏的单独的HTML页面。
这是CSV文件的图像:i.imgur.com/rcIPW .png
这是我到目前为止:
askfile = raw_input(什么是文件名?)
southpark = []
filename = open(askfile,'rU')
文件名中的行:
打印行[0:105]
filename.close()
以上打印了五行IDLE shell上的所有信息,但是我必须找到一个方法来分隔每行AND列并将其存储到列表中(我不知道该怎么做)。这是非常简单的代码,我知道我试图找出一种存储行和列的方式,然后我将使用一个函数(def)来首先将数据分配给HTML模板,然后创建一个HTML文件那个数据/模板...我到目前为止,我尝试通过网络搜索,但我只是不明白的东西。
我不允许使用任何可下载的模块,但我可以使用Python中构建的东西,如import csv或whatnot,但实际上它应该用几个功能,列表,字符串和循环来写。
一旦我弄清楚如何分隔行和列并存储它们,那么我可以在HTML模板中实现并创建文件。
我不是试图让我的硬件为我做,这只是我非常吸吮编程,所以任何帮助是赞赏!
BTW我使用的是Python 2.7.2,如果你想DL的CSV文件,请点击 here 。
更新:
好的,非常感谢!这有助于我了解每一行打印的内容以及该程序正在读取的信息。现在,由于我不得不在这个程序中使用函数,这就是我在想的。
每行(0-6)打印出单独的值,只是打印行功能打印出一个字符和所有相应的值,这是我需要的。我想要的是打印数据,如打印行,但我必须将这5个字符中的每一个存储在单独的列表中。
基本上打印行打印将所有5个字符与每个相应的属性一起分出来,如何将它们分成5个变量并将其存储为列表?
当我打印行[0 ]它只打印出名称,或打印行 1 只打印DOB。我正在考虑创建一个只需打印行并在一个循环中分为5个变量的def函数,然后另一个def函数接受这些变量/数据列表,并将它们与HTML模板组合在一起,最后我必须了解如何在Python中创建HTML文件。
对不起,如果我听起来很混乱,只是想弄清楚这一切。这是我的代码,现在它给出一个错误,有太多的值要解压缩,但我只是试图围绕,尝试不同的事情,看看他们是否工作。根据上面我想做的,我可能必须删除所有这些代码,并找到一种方法来重写它与列表类型函数,如.append或.strip等,我不是很熟悉..
import csv
original = file('southpark.csv','rU')
reader = csv.reader(原始)
#数据列表
名称,dob,描述,短语,个性,字符,apparel = []
count = 0
def southparkinfo():
读者中的行:
count + = 1
如果count == 0:
row [ 0] = name
打印行[0]#名称(例如Stan Marsh)
打印----------------
elif count == 1:
row [1] = dob
print row [1]#DOB
print----------------
elif count == 2:
row [2] =描述
打印行[2]#描述性的说法(例如尊重我的Authoritah!)
打印------ ----------
elif count == 3:
row [3] =短语
print row [3]#Catch Phrase(ex。 Mooom!)
print----------------
elif count == 4:
row [4] =个性
print row [4]#Personality(ex。Jewish)
print----------------
elif count == 5:
row [5] = character
print row [5]#特征(例如政治上不正确)
print----------------
elif count == 6:
row [6] = apparel
print row [6]#服装(例如红色手套)
return
reader.close()
首先,看看 CSV文档。
一旦你了解基础知识,看看这个代码。这应该让你从正确的路径开始:
import csv
original = file('southpark $ s
reader = csv.reader(原始)
读者中的行:
#将自行打印每行(名称中的所有列)
打印行
打印-----------------
#will打印第一列(仅限字符名称)
print row [0]
你想要 import csv
模块,所以你可以使用CSV文件类型。以通用换行模式打开文件,并使用csv.reader读取它。然后,您可以使用
循环开始遍历行,具体取决于您想要的。第一个打印行
将打印单个字符数据的一行(即:从名称到其服装类型的所有内容),如下所示:
['Stan Marsh','DOB:October 19th','Dude!','Aww#$%^!','Star Quarterback' '温迪','红手套'
-----------------
['凯尔·布罗夫洛夫斯基','DOB:5月26日' ,踢宝贝,你***!,犹太人,加拿大人,Ushanka]
-----------------
['Eric Theodore Cartman','DOB:7月1日','尊重我的Authroitah!','Mooom!','Big boned','政治
不正确','针织帽! ']
-----------------
['Kenny McCormick','DOB:3月22日','DOD:每隔一周','Mmff MmffF !!!','Mysterion!'
,'Orange Parka'
-----------------
[ 'Leopold Butters Stotch','DOB:比别人更年轻!','第四个朋友','混乱教授','stu
tter','innocent','nerdy']
-----------------
最后,第二个语句 print row [0]
将为您提供字符名称。您可以更改号码,您可以根据需要获取其他数据。记住,在CSV文件中,所有内容都从0开始,所以在你的情况下,只能上升到6,因为 A = 0,B = 1,C = 2等...
要更清楚地看到这些输出,最好注释掉一个 print
语句,以便更清楚地了解你正在抓住的内容。
-----------------
Stan Marsh
--- --------------
凯尔Broflovski
-----------------
埃里克·西奥多·卡特曼
-----------------
肯尼·麦考密克
-----------------
Leopold Butters Stotch
注意我投入了打印-----------------
,所以你可以看到不同的输出。
希望这有助于你让你开始。
编辑 回答你的第二个问题:最简单的方式(虽然可能不是最好的方式)抓住所有单个角色的信息将是这样做:
import csv
original = file('southpark.csv','rU')
reader = csv.reader(original)
stan = reader。 next()
kyle = reader.next()
eric = reader.next()
kenny = reader.next()
butters = reader.next()
print eric
哪些输出:
['埃里克·西奥多·卡特曼','DOB:7月1日','尊重我的Authroitah!','Mooom!','Big boned','政治上不正确' 'knit-cap!']
请注意,如果您的CSV被修改,字符被移动(例如:butters被移动到顶部),您将输出另一个字符的信息。
Basically my problem is this: I have a CSV excel file with info on Southpark characters and I and I have an HTML template and what I have to do is take the data by rows (stored in lists) for each character and using the HTML template given implement that data to create 5 seperate HTML pages with the characters last names.
Here is an image of the CSV file: i.imgur.com/rcIPW.png
This is what I have so far:
askfile = raw_input("What is the filename?")
southpark = []
filename = open(askfile, 'rU')
for row in filename:
print row[0:105]
filename.close()
The above prints out all the info on the IDLE shell in five rows but I have to find a way to separate each row AND column and store it into a list (which I don't know how to do). It's pretty rudimentary code I know I'm trying to figure out a way to store the rows and columns first, then I will have to use a function (def) to first assign the data to the HTML template and then create an HTML file from that data/template..and I'm so far a noob I tried searching through the net but I just don't understand the stuff.
I am not allowed to use any downloadable modules but I can use things built in Python like import csv or whatnot, but really its supposed to be written with a couple functions, list, strings, and loops..
Once I figure out how to separate the rows and columns and store them then I can work on implementing into HTML template and creating the file.
I'm not trying to have my HW done for me it's just that I pretty much suck at programming so any help is appreciated!
BTW I am using Python 2.7.2 and if you want to DL the CSV file click here.
UPDATE:
Okay, thanks a lot! That helped me understand what each row was printing and what info is being read by the program. Now since I have to use functions in this program somehow this is what I was thinking.
Each row (0-6) prints out separate values, but just the print row function prints out one character and all his corresponding values which is what I need. What I want is to print out data like "print row" would but I have to store each of those 5 characters in a separate list.
Basically "print row" prints out all 5 characters with each of their corresponding attributes, how can I split each of them into 5 variables and store them as a list?
When I do print row[0] it only prints out the names, or print row1 only prints the DOB. I was thinking of creating a def function that takes only print "row" and splits into 5 variables in a loop and then another def function takes those variables/lists of data and combines them with the HTML template, and at the end I have to figure out how to create HTML files in Python..
Sorry if I sound confusing just trying to make sense of it all. This is my code right now it gives an error that there are too many values to unpack but I am just trying to fiddle around and try different things and see if they work. Based on what I wanted to do above I will probably have to delete all most of this code and find a way to rewrite it with list type functions like .append or .strip, etc which I am not very familiar with..
import csv
original = file('southpark.csv', 'rU')
reader = csv.reader(original)
# List of Data
name, dob, descript, phrase, personality, character, apparel = []
count = 0
def southparkinfo():
for row in reader:
count += 1
if count == 0:
row[0] = name
print row[0] # Name (ex. Stan Marsh)
print "----------------"
elif count == 1:
row[1] = dob
print row[1] # DOB
print "----------------"
elif count == 2:
row[2] = descript
print row[2] # Descriptive saying (ex. Respect My Authoritah!)
print "----------------"
elif count == 3:
row[3] = phrase
print row[3] # Catch Phrase (ex. Mooom!)
print "----------------"
elif count == 4:
row[4] = personality
print row[4] # Personality (ex. Jewish)
print "----------------"
elif count == 5:
row[5] = character
print row[5] # Characteristic (ex. Politically incorrect)
print "----------------"
elif count == 6:
row[6] = apparel
print row[6] # Apparel (ex. red gloves)
return
reader.close()
First and foremost, have a look at the CSV docs.
Once you understand the basics take a look at this code. This should get you started on the right path:
import csv
original = file('southpark.csv', 'rU')
reader = csv.reader(original)
for row in reader:
#will print each row by itself (all columns from names up to what they wear)
print row
print "-----------------"
#will print first column (character names only)
print row[0]
You want to import csv
module so you can work with the CSV filetype. Open the file in universal newline mode and read it with csv.reader. Then you can use a for
loop to begin iterating through the rows depending on what you want. The first print row
will print a single line of all a single character's data (ie: everything from their name up to their clothing type) like so:
['Stan Marsh', 'DOB: October 19th', 'Dude!', 'Aww #$%^!', 'Star Quarterback', 'Wendy', 'red gloves']
-----------------
['Kyle Broflovski', 'DOB: May 26th', 'Kick the baby!', 'You ***!', 'Jewish', 'Canadian', 'Ushanka']
-----------------
['Eric Theodore Cartman', 'DOB: July 1', 'Respect My Authroitah!', 'Mooom!', 'Big-boned', 'Political
ly incorrect', 'Knit-cap!']
-----------------
['Kenny McCormick', 'DOB: March 22', 'DOD: Every other week', 'Mmff Mmff', 'MMMFFF!!!', 'Mysterion!'
, 'Orange Parka']
-----------------
['Leopold Butters Stotch', 'DOB:Younger than the others!', 'The 4th friend', 'Professor chaos', 'stu
tter', 'innocent', 'nerdy']
-----------------
Finally, the second statement print row[0]
will provide you with the character names only. You can change the number and you'll be able to grab the other data as necessary. Remember, in a CSV file everything starts at 0, so in your case you can only go up to 6 because A=0, B=1, C=2, etc...
To see these outputs more clearly, it's probably best if you comment out one of the print
statements so you get a clearer picture of what you are grabbing.
-----------------
Stan Marsh
-----------------
Kyle Broflovski
-----------------
Eric Theodore Cartman
-----------------
Kenny McCormick
-----------------
Leopold Butters Stotch
Note I threw in that print "-----------------"
so you would be able to see the different outputs.
Hope this helps you get you off to a start.
Edit To answer your second question: The easiest way (although probably not the best way) to grab all of a single character's info would be to do something like this:
import csv
original = file('southpark.csv', 'rU')
reader = csv.reader(original)
stan = reader.next()
kyle = reader.next()
eric = reader.next()
kenny = reader.next()
butters = reader.next()
print eric
which outputs:
['Eric Theodore Cartman', 'DOB: July 1', 'Respect My Authroitah!', 'Mooom!', 'Big-boned', 'Politically incorrect', 'Knit-cap!']
Take note that if your CSV is modified such that the order of the characters are moved (ex: butters is moved to top) you will output the info of another character.
这篇关于如何将行从CSV文件存储到Python中并使用HTML打印数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!