user14129127
user14129127

Reputation:

How to get specific column value from .csv Python3?

I have a .csv file with Bitcoin price and market data, and I want to get the 5th and 7th columns from the last row in the file. I have worked out how to get the last row, but I'm not sure how to extract columns (values) 5 and 7 from it. Code:

with open('BTCAUD_data.csv', mode='r') as BTCAUD_data:
    writer = csv.reader(BTCAUD_data, delimiter=',')
    data = list(BTCAUD_data)[-1]
    print(data)

Edit: How would I also add column names, and would adding them help me? (I have already manually put the names into individual columns in the first line of the file itself)

Edit #2: Forget about the column names, they are unimportant. I still don't have a working solution. I have a vague idea that I'm not actually reading the file as a list, but rather as a string. (This means when I subscript the data variable, I get a single digit, rather than an item in a list) Any hints to how I read the line as a list?

Edit #3: I have got everything working to expectations now, thanks for everyone's help :)

Upvotes: 0

Views: 1394

Answers (2)

Patrick Artner
Patrick Artner

Reputation: 51683

Your code never uses the csv-reader. You can do so like this:

import csv

# This creates a file with demo data
with open('BTCAUD_data.csv', 'w') as f:
    f.write(','.join( f"header{u}" for u in range(10))+"\n")
    for l in range(20):
        f.write(','.join( f"line{l}_{c}" for c in range(10))+"\n")


# this reads and processes the demo data
with open('BTCAUD_data.csv', 'r', newline="") as BTCAUD_data:
    reader = csv.reader(BTCAUD_data, delimiter=',')
    # 1st line is header
    header =  next(reader)
    # skip through the file, row will be the last line read
    for row in reader:
        pass

print(header)
print(row)

#  each row is a list  and you can index into it
print(header[4], header[7])
print(row[4], row[7])

Output:

['header0', 'header1', 'header2', 'header3', 'header4', 'header5', 'header6', 'header7', 'header8', 'header9']
['line19', 'line19', 'line19', 'line19', 'line19', 'line19', 'line19', 'line19', 'line19', 'line19']
header4 header7
line19_4 line19_7 

Upvotes: 0

VGB
VGB

Reputation: 507

Better use pandas for handling CSV file.

import pandas as pd
df=pd.read_csv('filename')

df.column_name will give the corresponding column enter image description here

If you read this csv file into df and try df.Year will give you the Year column.

Upvotes: 0

Related Questions