简体   繁体   English

如何将CSV文件中的行存储到Python中并使用HTML打印数据

[英]How to Store Rows from CSV File into Python and Print Data with HTML

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. 基本上我的问题是:我有一个CSV excel文件,其中包含有关Southpark字符的信息,我和我都有一个HTML模板,而我要做的就是按行(存储在列表中)获取每个字符的数据并使用HTML模板给定的实现该数据以创建5个带有字符姓氏的单独HTML页面。

Here is an image of the CSV file: i.imgur.com/rcIPW.png 这是CSV文件的图像: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). 上面将IDLE外壳上的所有信息打印成五行,但是我必须找到一种方法来分隔每行AND列并将其存储到列表中(我不知道该怎么做)。 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. 这是非常基本的代码,我知道我想先找出一种存储行和列的方法,然后我必须使用函数(def)首先将数据分配给HTML模板,然后从该数据/模板..到目前为止,我还是一个菜鸟,我尝试通过网络进行搜索,但我只是不了解这些内容。

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.. 我不允许使用任何可下载的模块,但可以使用Python内置的功能(例如import csv或whatnot),但实际上应该由几个函数,列表,字符串和循环编写。

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. 一旦弄清楚如何分隔行和列并存储它们,便可以将其实现为HTML模板并创建文件。

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 . 顺便说一句,我正在使用Python 2.7.2,如果您想DL CSV文件,请单击此处


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. 每行(0-6)都会打印出单独的值,但是仅打印行功能会打印出一个字符和他所需要的所有相应值。 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. 我想要的是像“打印行”那样打印出数据,但是我必须将这5个字符中的每一个存储在单独的列表中。

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? 基本上,“打印行”会打印出所有5个具有相应属性的字符,我如何将每个字符分成5个变量并将其存储为列表?

When I do print row[0] it only prints out the names, or print row 1 only prints the DOB. 当我打印第[0]行时,它仅打印出名称,或者打印第1行仅打印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.. 我当时想创建一个仅打印“行”并在一个循环中拆分为5个变量的def函数,然后另一个def函数将这些变量/数据列表与HTML模板结合起来,最后我必须了解如何在Python中创建HTML文件。

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.. 基于我上面想要做的事情,我可能必须删除所有这些代码,并找到一种方法,用我不太熟悉的列表类型函数(如.append或.strip等)重写它。

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 . 首先,请看CSV文档

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. 您要import csv模块,以便可以使用CSV文件类型。 Open the file in universal newline mode and read it with csv.reader. 在通用换行模式下打开文件,然后使用csv.reader进行读取。 Then you can use a for loop to begin iterating through the rows depending on what you want. 然后,您可以使用for循环根据您的需要开始遍历各行。 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: 第一print row打印所有单个字符数据的一行(即,从名称到服装类型的所有内容),如下所示:

['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. 最后,第二条语句print row[0]将仅为您提供字符名称。 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. 请记住,在CSV文件中,所有内容都从0开始,因此,由于A=0, B=1, C=2, etc...您最多只能增加6。为了更清楚地看到这些输出,最好是您可以注释掉其中的一份print声明,以便更清晰地了解要抓取的内容。

-----------------
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. 请注意,我在print "-----------------"加上了print "-----------------"这样您就可以看到不同的输出。 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从而移动了字符的顺序(例如,黄油被移到顶部),则将输出另一个字符的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中使用 Pandas 打印 csv 文件中的所有行/数据? - How to print all rows/data from csv file with Pandas in Python? 如何将 CSV 文件中的数据作为文本读取并打印到 HTML 文件中 - How To Read and Print Data from CSV file into HTML File as Text 如何从 a.CSV 文件中找到前 10 行 AWND,并使用 Python 将结果存储在新的.CSV 文件中? - How to find the top 10 rows of AWND from a .CSV file and store the result in a new .CSV file using Python? 如何将 csv 文件中特定列的数据存储到 Python 中的列表中 - How to store data of a specific column from a csv file to a list in Python 如何在 Python 中解析以下 HTML 文件并将所有问题作为列存储到 csv 中,并将其答案作为行 - How to parse following HTML file in Python and store into csv with all questions as columns and their answers as rows 如何使用Python从html表中通过Web抓取数据并将其存储在csv文件中。 我可以提取某些部分,但不能提取其他部分 - How to web scrape data using Python from an html table and store it in a csv file. I am able to extract some parts but not the others 如何在python中从csv文件的最小值和最大值打印所有列和行 - How to print all columns and rows from a csv file's min and max value in python Python-如何从csv文件中使用常见单词打印多行? - Python-how to print multiple rows with a common word from a csv file? 如何从 csv 文件中读取行并使用 Python 代码将其存储在数组中? - How do you read rows from a csv file and store it in an array using Python codes? 如何从 csv 文件中打印随机 n 行? - How to print random n rows from a csv file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM