Python Forum

Full Version: Doesn't look like a dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
what does summary.txt look like?
would you please display a few rows of raw data.
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.
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?
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 average
That 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.
very nice!!
Thank you

Ran they code and received is an error

invalid literal for int() with base 10:
it looks your csv file is not well formed.
There are at least 2 lines with problems

in the sample data you have
Output:
135,2018/07/28 02:27,1,,123,,,,,,,,,,,,,,
and also in the other thread sample data include
Output:
144,2018/07/28 04:14,1,,79,,,,,,,,,,,,,,
note that there is extra field between 1 and Glucose data
For these rows (and probably others like these) this will cause problem to convert to int, i,e, int('') will cause that error you get.
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.
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
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
123-45-6789
ID Time Record Type Historic Glucose (mg/dL) 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 N/A N/A Previous Time Updated Time
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
140 2018/07/28 03:12 0 79
141 2018/07/28 03:27 0 71
142 2018/07/28 03:42 0 69
143 2018/07/28 03:57 0 72
144 2018/07/28 04:14 1 79
147 2018/07/28 05:16 6 2018/07/28 05:16 2018/07/28 05:16
148 2018/07/28 04:12 0 83
149 2018/07/28 04:27 0 86
150 2018/07/28 04:42 0 85
151 2018/07/28 04:57 0 86
152 2018/07/28 05:12 0 90
153 2018/07/28 05:27 0 94

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):
File "C:/Users/gary/PycharmProjects/sqlite/readfile.py", line 14, in <module>
data = {int(row['ID']): row for row in reader}
File "C:/Users/gary/PycharmProjects/sqlite/readfile.py", line 14, in <dictcomp>
data = {int(row['ID']): row for row in reader}
KeyError: 'ID'

I have programmed in php for years, so I thought I could transfer to python with ease. Boy was I mistaken.
Pages: 1 2