简体   繁体   中英

Trying to read in data from a CSV file, list index out of range?

I have no idea why this isn't working, I've done this before with CSV files and it worked. The file has no blank lines or blank values and the data is separated by commas. This is what I have:

#Create empty lists for data.
Date = []
Open = []
High = []
Low = []
Close = []
Adj_Close = []
Volume = []

#Fill lists with data.
with open("AAPL_train.csv", "r") as infile:
    for lines in infile:
        lines = lines.split(",")
        Date.append(lines[0])
        Open.append(lines[1])
        High.append(lines[2])
        Low.append(lines[3])
        Close.append(lines[4])
        Adj_Close.append(lines[5])
        Volume.append(lines[6])

My error code is reading that it goes out of index at the Open.append(lines[1]) line.

Then here is a sample of the data to show you what it looks like.

Any ideas? Thank you.

Edited to add: The error I'm getting is IndexError: list index out of range on line 18, and when I try to do a print line after each loop, I get nothing but the error.

import csv

#Create empty lists for data.
Date = []
Open = []
High = []
Low = []
Close = []
Adj_Close = []
Volume = []

#Fill lists with data.
with open("AAPL_train.csv", "r") as infile:
    csv_reader = csv.reader(infile, delimiter=','):
        # skip header
        next(csv_reader)
        for row in infile:
            Date.append(row[0])
            Open.append(row[1])
            High.append(row[2])
            Low.append(row[3])
            Close.append(row[4])
            Adj_Close.append(row[5])
            Volume.append(row[6])

I've updated your code to use the inbuilt python csv package. It makes life easier when dealing with CSV and its generator should help prevent any memory problems down the line!

I also added the next(csv_reader) call in order to skip the header in your csv data. I made the assumption this was data you may not actually want to record.

Try using pandas:

import pandas as pd

df = pd.read_csv(AAPL_train.csv)

Date = df['Date'].tolist()
Open = df['Open'].tolist()
High = df['High'].tolist()
Low = df['Low'].tolist()
Close = df['Close'].tolist()
Adj_Close = df['Adj_Close'].tolist()
Volume = df['Volume'].tolist()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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