简体   繁体   English

在python中读取csv文件并创建数据库

[英]Reading a csv file in python and creating database

I'm working on a function where I need to accept a CSV file name as a string, open and read it, create a database, and then return the database. 我正在使用需要接受CSV文件名作为字符串,打开并读取它,创建数据库然后返回数据库的函数。 The attempt I have so far seems to have the correct logic but it is giving me the error "No such file or directory 'filename.csv'. The files I'm reading are called file0.csv, file1.csv, etc. I'll include an example of one of them with my code below. Does anyone have any advice on how to fix this? Thanks 到目前为止,我进行的尝试似乎具有正确的逻辑,但是却给我错误“没有此类文件或目录'filename.csv'。我正在读取的文件称为file0.csv,file1.csv等。我我将在下面的代码中包含其中一个示例。有人对如何解决此问题有任何建议吗?

Edit: I realize that what I included below is an example of the database. 编辑:我意识到我下面包含的是数据库的示例。 Apparantly the first line of the file is the header row and the code I have now is reading the header row when it shouldn't be. 显然,文件的第一行是标题行,而我现在拥有的代码正在读取不应包含的标题行。 Here is the updated code below 这是下面的更新代码

Code: 码:

def read_file(filename):
    thefile = open(filename)
    data = []
    for line in thefile: 
        data.append(line)
    thefile.close()

    return data



    Example database:

{'Leonardo  da  Vinci': [('Mona Lisa', 1503,    
76.8, 53.0, 'oil paint', 'France'), ('The   
Last Supper', 1495, 460.0, 880.0, 'tempera',    
'Italy')]}

Let's look at just the first two lines of your code: 让我们看一下代码的前两行:

def read_file(filename):
    thefile = open('filename.csv')

I surmise that, since you want to be able to process more than one file with this code you want to be able to call read_file substituting various filenames in place of filename . 我推测,由于您希望能够使用此代码处理多个文件,因此您希望能够调用read_file代替各种filename来代替filename Correct? 正确?

OK, then one flaw in the code is that filename in the first line is a variable but 'filename.csv' is a literal. 行,则在代码的一个缺陷是, 文件名中的第一行是一个可变的但“filename.csv”是文字。 This means that no matter what you put for filename in the first line it will NOT change the literal. 这意味着无论您在第一行中为文件名添加什么内容,都不会更改文字。 To do that the second line would have to be, for instance, 为此,第二行必须是例如

thefile = open ('%s.csv' % filename, 'r')

This would put what's in the filename variable in place of the %s and do what you seem to want. 这会将文件名变量中的内容替换为%s,并执行您似乎想要的操作。

What most respondents are yammering about: Your script (ie, the Python code) might be in one disc folder or directory but the files you want to process might be in a different folder or directory. 大多数受访者都在抱怨:您的脚本(即Python代码)可能位于一个光盘文件夹或目录中,但您要处理的文件可能位于另一个文件夹或目录中。 When you run a script without telling it where to look for files it will assume that you mean in the folder where it's running. 当您在不告诉脚本在哪里寻找文件的情况下运行脚本时,将假定您的意思是在运行脚本的文件夹中。 At your stage of the game, the easiest thing to do is to put the Python script and the files it needs all in the same folder, and then run then in that same folder. 在游戏的阶段,最简单的方法是将Python脚本及其需要的文件全部放在同一文件夹中,然后在同一文件夹中运行。

Maybe you want something like this? 也许您想要这样的东西? It opens your file (provided you find its correct location), accumulates its lines and returns the set of lines in your file. 它会打开您的文件(假设找到正确的位置),累积其行并返回文件中的行集。

def read_file(filename):
    thefile = open('file0.csv', 'r')
    lines = []
    for line in thefile: 
        lines.append(line)
    thefile.close()

    return lines

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM