python - CSV find max in column and append new data -
i asked question 2 hours ago regarding reading , writing of data website. i've spent last 2 hours since trying find way read maximum date value column 'a' of output, comparing value refreshed website data, , appending new data csv file without overriding old ones or creating duplicates.
the code 100% working this:
import requests symbol = "mtgoxusd" url = 'http://api.bitcoincharts.com/v1/trades.csv?symbol={}'.format(symbol) data = requests.get(url) open("trades_{}.csv".format(symbol), "r+") f: f.write(data.text)
i've tried various ways of finding maximum value of column 'a'. i've tried bunch of different ways of using "dict" , other methods of sorting/finding max, , using pandas , numpy libs. none of seem work. point me in direction of decent way find maximum of column .csv file? thanks!
i'll give 2 answers, 1 returns max value, , 1 returns row csv includes max value.
import csv import operator op import requests symbol = "mtgoxusd" url = 'http://api.bitcoincharts.com/v1/trades.csv?symbol={}'.format(symbol) csv_file = "trades_{}.csv".format(symbol) data = requests.get(url) open(csv_file, "w") f: f.write(data.text) open(csv_file) f: next(f) # discard first row file -- see notes max_value = max(row[0] row in csv.reader(f)) open(csv_file) f: next(f) # discard first row file -- see notes max_row = max(csv.reader(f), key=op.itemgetter(0))
notes:
max()
can directly consume iterator, ,csv.reader()
gives iterator, can pass in. i'm assuming might need throw away header line showed how that. if had multiple header lines discard, might want useislice()
itertools
module.in first one, use "generator expression" select single value each row, , find max. similar "list comprehension" doesn't build whole list, lets iterate on resulting values.
max()
consumes iterable , max value.max()
can usekey=
argument specify "key function". use key function value , use value figure max... value returnedmax()
unmodified original value (in case, row value csv). in case, key function manufacturedoperator.itemgetter()
... pass in column want, ,operator.itemgetter()
builds function gets column.
the resulting function equivalent of:
def get_col_0(row): return row[0] max_row = max(csv.reader(f), key=get_col_0)
or, people use lambda
this:
max_row = max(csv.reader(f), key=lambda row: row[0])
but think operator.itemgetter()
convenient , nice read. , it's fast.
- i showed saving data in file, pulling file again. if want go through data without saving anywhere, need iterate on lines.
perhaps like:
text = data.text rows = [line.split(',') line in text.split("\n") if line] rows.pop(0) # rid of first row data max_value = max(row[0] row in rows) max_row = max(rows, key=op.itemgetter(0))
- i don't know column want... column "a" might column 0 used 0 in above. replace column number like.
Comments
Post a Comment