Doesn't look like a dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Doesn't look like a dictionary (/thread-14688.html) Pages:
1
2
|
Doesn't look like a dictionary - gehrenfeld - Dec-12-2018 with open('summary.txt') as f: reader = csv.DictReader(f, delimiter=',') data = {int(row['ID']): row for row in reader} f.close() print(data.get(1065, None)) OrderedDict([('ID', '1065'), ('Time', '2018/08/06 07:59'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '72'), ('Scan Glucose (mg/dL)', ''), ('Non-numeric Rapid-Acting Insulin', ''), ('Rapid-Acting Insulin (units)', ''), ('Non-numeric Food', ''), ('Carbohydrates (grams)', ''), ('Non-numeric Long-Acting Insulin', ''), ('Long-Acting Insulin (units)', ''), ('Notes', ''), ('Strip Glucose (mg/dL)', ''), ('Ketone (mmol/L)', ''), ('N/A', ''), ('Previous Time', ''), ('Updated Time', '')])Input file: 132,2018/07/28 01:41,0,141,,,,,,,,,,,,,,, 133,2018/07/28 01:56,0,133,,,,,,,,,,,,,,, 134,2018/07/28 02:11,0,126,,,,,,,,,,,,,,, 135,2018/07/28 02:27,1,,123,,,,,,,,,,,,,, 137,2018/07/28 02:27,0,126,,,,,,,,,,,,,,, 138,2018/07/28 02:42,0,119,,,,,,,,,,,,,,, 139,2018/07/28 02:57,0,96,,,,,,,,,,,,,,,This does not look like it is a dictionary. I thought it was like ID:1065 I am I using the correct csv.DictReader? RE: Doesn't look like a dictionary - Larz60+ - Dec-12-2018 what does summary.txt look like? would you please display a few rows of raw data. RE: Doesn't look like a dictionary - ichabod801 - Dec-12-2018 It's an ordered dictionary: it works just like a dictionary, but it remembers the order that keys were put into it. That way if you loop through it, you always loop through the keys in the same order, which is not guaranteed with a normal dictionary. RE: Doesn't look like a dictionary - gehrenfeld - Dec-12-2018 Maybe I am not approaching this project correctly. I want to import the csv file which is a download from my diabetes meter. Then I want to take the Historic Glucose (mg/dL) number and add them all up so I can get an average. Is a list a better way or is dictionary the way to go? RE: Doesn't look like a dictionary - ichabod801 - Dec-12-2018 What you've got works fine. glucose_levels = [int(datum['Historic Glucose (mg/dL)']) for datum in data] # extracts the glucose levels as numbers print(sum(glucose_levels) / len(glucose_levels)) # prints the averageThat assumes all the rows have a glucose level, and that you are using version 3.0+. If either of those is not true, they can easily be fixed. You can subset out the rows with no glucose level with filter, and use use float(len(glucose_levels)) in earlier versions of Python.
RE: Doesn't look like a dictionary - gehrenfeld - Dec-12-2018 very nice!! Thank you Ran they code and received is an error invalid literal for int() with base 10: RE: Doesn't look like a dictionary - buran - Dec-12-2018 it looks your csv file is not well formed. There are at least 2 lines with problems in the sample data you have and also in the other thread sample data include note that there is extra field between 1 and Glucose dataFor these rows (and probably others like these) this will cause problem to convert to int, i,e, int('') will cause that error you get.
RE: Doesn't look like a dictionary - gehrenfeld - Dec-12-2018 Quote:('ID', '1065'), ('Time', '2018/08/06 07:59'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '72'), ('Scan Glucose (mg/dL)', ''), ('Non-numeric Rapid-Acting Insulin', ''), ('Rapid-Acting Insulin (units)', ''), ('Non-numeric Food', ''), ('Carbohydrates (grams)', ''), ('Non-numeric Long-Acting Insulin', ''), ('Long-Acting Insulin (units)', ''), ('Notes', ''), ('Strip Glucose (mg/dL)', ''), ('Ketone (mmol/L)', ''), ('N/A', ''), ('Previous Time', ''), ('Updated Time', '')])these are the fields that need to be int ID, Historic Glucose (mg/dL),Scan Glucose (mg/dL) Any way to just import those? The rest I don't really care about. The download software of the meter does not put a 0 if there is no value. Don't know how to get around that. RE: Doesn't look like a dictionary - buran - Dec-12-2018 In most of the rows Historic Glucose (mg/dL) is number (but as str when you read it from the file) and Scan Glucose (mg/dL) is empty i.e. ('') in the rows I show you it's the reverse. Are you sure that's correct, it looks like bug to me RE: Doesn't look like a dictionary - gehrenfeld - Dec-12-2018 Let me start over and give you the code and file I work with. I have used so many different formats. Here is what the import file looks like from the meter download. I remove my name and ssn# before I import it. Quote:Gary Ehrenfeld with open('summary.txt') as f: reader = csv.DictReader(f, delimiter='\t') data = {int(row['ID']): row for row in reader} f.close()The error I get: Quote:Traceback (most recent call last): I have programmed in php for years, so I thought I could transfer to python with ease. Boy was I mistaken. |