0

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 :)

2
  • Can you show me the names of the column you put at the first line? Commented Nov 15, 2020 at 9:56
  • @deecue Raw Price Change, Price Change Percent, Weighted Average, Last Price, Last Qty, Bid Price, Bid Qty, Ask Price, Ask Qty, Open Price, High Price, Low Price, Trading Volume, Quote Volume Commented Nov 15, 2020 at 10:00

2 Answers 2

0

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 
Sign up to request clarification or add additional context in comments.

5 Comments

Ok thanks heaps for your help! Just out of curiosity, how would I go about ea if I didn't want to have named columns, and just get the output (I'm still trying to get my head around reading/writing from files!)
@proton You do not have named colums with the default csv.reader - you get a list per row in your file and index into it by index ( 4 and 7 here). I don't quite get what you are asking in your comment.
Sorry I'm just really confused by all this (mainly the first with open section) I will have a deeper look into the code and try and understand it.
@proton - the first with open(..) creates the file , the second part reads it back in and processes it. You did not provide how your data looks like, so I created sample data myself
ohhh ok! Thanks for clarifying, makes much more sense now.
0

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.

2 Comments

Pandas has a big learning curve - if it is a tool you know thats ok, for geting 2 values of the last row the csv module is more then adequat and takes less efford.
The pandas option seems easier to me, 14 lines versus 3....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.