Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

read a csv file containing data separated by a delimiter in Python Flask

I'm trying to read a csv file, using the separator ',' but when I do line [1] I have an indexing error: I go out of range. For what reason? what follows is a part of the code that manages the split but does not work.

with open(fileLocation, newline='') as fileCsv:
    reader = csv.reader(fileCsv, delimiter=',')
    first = True
    second = False
    prev = fileCsv.readline()
    reader= list(reader)
    for line in reader:
        if first:
            first = False
            second = True
            continue
        
        email = line[20].lower()
        nameSurname = line[0]
        msg=''
        if nameSurname == "" and email == "":
            return render_template('LoadCSV.html', login=loginStr(), avviso='err'+msg, fileLocation=fileLocation, category=app.category)
        data = line[3][0:10].replace("/","-")
        group = line[1]
        startingTime = line[16][11:]
        EndingTime = line[3][11:]
        language = line[10]

This instead is an example of the data contained in the file:

Name Surname,Student,"Assessment timed out","2019/10/31 12:10:08",19,72,26,Fail,000000000,000000000000000,"English B1",Stefano,"2019/10/29 08:45:15",IDNUMBER,10.30.6.5,157.27.1.13,"2019/10/31 11:24:56",2706,,,[email protected],,,,,,,,,"English B1"

Name2 Surname2,Student,"Assessment timed out","2019/10/31 12:10:08",19,72,26,Fail,000000000,000000000000000,"Enghlish B1",Stefano,"2019/10/29 08:45:15",IDNUMBER,10.30.6.5,157.27.1.13,"2019/10/31 11:24:56",2706,,,[email protected],,,,,,,,,"English B1"

question from:https://stackoverflow.com/questions/65644718/read-a-csv-file-containing-data-separated-by-a-delimiter-in-python-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...