Python Forum
Doesn't look like a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Doesn't look like a dictionary
#1
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?
Gary
Reply
#2
what does summary.txt look like?
would you please display a few rows of raw data.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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?
Gary
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
very nice!!
Thank you

Ran they code and received is an error

invalid literal for int() with base 10:
Gary
Reply
#7
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
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.
Gary
Reply
#9
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
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.
Gary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Line of code to show dictionary doesn't work MaartenRo 2 2,397 Jul-28-2020, 03:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020