简体   繁体   English

使用Python,Flask读取CSV文件时出错

[英]Error Reading CSV file using Python, Flask

I'm very new to python and flask. 我对python和flask非常陌生。 I simply wanted to read a CSV file but it's giving me an error of "FileNotFoundError: [Errno 2] No such file or directory: 'Dog-Data.csv'" everytime I try to run run.py 我只是想读取CSV文件,但是每次我尝试运行run.py时,都会出现错误“ FileNotFoundError:[Errno 2]没有这样的文件或目录:'Dog-Data.csv'”

Here are my file order 这是我的文件顺序

DD\
    static\
        (bunch of image files)
    templates\
        (bunch of template files)
    __init__.py
    views.py
    Dog-Data.csv

views.py views.py

from flask import render_template
from app import app
import csv

@app.route('/')
@app.route('/Home')
def home():
    return render_template("Home.html",title='Home')

@app.route('/MakeaMatch')
def makeamatch():
    return render_template("MakeaMatch.html",title='Make a Match!')

@app.route('/Game')
def game():
    return render_template("Game.html",title='Game')

@app.route('/ListofDogs')
def listofdogs():
    return render_template("ListofDogs.html",title='List of Dogs')

@app.route('/About')
def about():
    return render_template("About.html", title='About')

@app.route('/Contact')
def contact():
    return render_template("Contact.html", title='Contact')

@app.route('/MatchResult')
def matchresult():
    class DogBreed:
        def __init__(self, br, per, si, lif, hou, cli):
            self.breed = br
            self.personality = per
            self.size = si
            self.lifestyle = lif
            self.housing = hou
            self.climate = cli
            self.match = 0

    #List that will contain class DogBreed
    ListOfBreeds = []

    data_file = open('Dog-Data.csv')
    csv_file = csv.reader(data_file)

    for row in csv_file:
        #print (row) #will print the csv file
        #print (row[2]) #will print element of that row
        dog_breed = DogBreed(row[0],row[1].lower().split(", "),row[2].lower(),row[3].lower(),row[4].lower(),row[5].lower())
        ListOfBreeds.append(dog_breed)

    data_file.close()

    #MORE CODES HERE. OMITTED BECAUSE I DON'T THINK IT'S RELEVANT

    return render_template("MatchResult.html",title='Match Result',posts=ListOfBreeds)

The webpage loads and templates shows up fine if I comment out the lines involving CSV. 如果我注释掉涉及CSV的行,则网页加载和模板显示会很好。 However, it doesn't of course show the result I want it too. 但是,它当然也不会显示我想要的结果。

I've also tried putting the Dog-Data.csv into the static folder and used 我也尝试过将Dog-Data.csv放入静态文件夹并使用

data_file = open('static/Dog-Data.csv')

but this didn't work either. 但这也不起作用。

Thanks so much for help. 非常感谢您的帮助。

Have you tried to give the full path instead of just a relative path? 您是否尝试给出完整路径而不是相对路径?

Python sometimes takes the working directory as a "home" path, which might or might not be the same as your view.py is situated in. Python有时会将工作目录视为“主”路径,该路径可能与您的view.py所在的路径相同或不同。

ok so really simple mistake that took hours for me to solve. 好,这么简单的错误让我花了几个小时解决。 I should be passing it from the root folder which means I should've put 我应该从根文件夹传递它,这意味着我应该放

data_file = open('app/Dog-Data.csv')

and now it works. 现在可以了。 T__T T__T

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

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