Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV import is not working
#1
Here is the code I am using
with open('summary.txt') as f:
    reader = csv.DictReader(f, delimiter=',')
    data = [row for row in reader]

for i in data:
        print(i)
The file contains 10,000 lines so I won't post all of the output.
The problem is that the output starts somewhere in the middle of the file.

')])
OrderedDict([('ID', '8346'), ('Time', '2018/11/03 20:54'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '114'), ('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', '')])
OrderedDict([('ID', '8347'), ('Time', '2018/11/03 21:09'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '116'), ('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', '')])
OrderedDict([('ID', '8348'), ('Time', '2018/11/03 21:24'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '127'), ('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', '')])
OrderedDict([('ID', '8349'), ('Time', '2018/11/03 21:39'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '136'), ('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', '')])
OrderedDict([('ID', '8350'), ('Time', '2018/11/03 21:54'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '148'), ('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', '')])
OrderedDict([('ID', '8351'), ('Time', '2018/11/03 22:09'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '163'), ('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', '')])
OrderedDict([('ID', '8352'), ('Time', '2018/11/03 22:24'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '170'), ('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', '')])
OrderedDict([('ID', '8353'), ('Time', '2018/11/03 22:39'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '168'), ('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', '')])
Not sure where the ')]) is coming from or why it doesn't show the first part of the file.
Here are the first 5 lines of the input file file.
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,,,,,,,,,,,,,,
Thanks
Gary
Reply
#2
try
with open('summary.txt') as f:
    reader = csv.DictReader(f, delimiter=',')
    print(next(reader))
this will print the first line in reader. I guess you print in cmd.exe and simply cannot scroll back to begining of your data
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
#3
You were correct.

Thanks for the help
Gary
Reply
#4
I am new to python so bare with me, please

I have a dictionary named data. I want to find and print a certain line in the dictionary.

I tried this:

print(data[id][10652])
Thinking I would get to this

([('ID', '10652'), ('Time', '2018/12/11 04:30'), ('Record Type', '0'), ('Historic Glucose (mg/dL)', '121'), ('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', '')])

But instead I get TypeError: list indices must be integers or slices, not builtin_function_or_method

How do I find and search thins in a dictionary? Believe it or not I have taken two classes in Python. Sad
Gary
Reply
#5
Can you get 5-10 lines of runnable code that reproduce your issue? As it stands, it looks like you're leaving important context out.
Reply
#6
If you have sorted data without missing records, you can calculate index of searched row like
data = ( [('ID', '10652'), ('Time', '2018/12/11 02:30')],  \
         [('ID', '10653'), ('Time', '2018/12/11 03:30')],  \
         [('ID', '10654'), ('Time', '2018/12/11 04:30')],  \
         [('ID', '10655'), ('Time', '2018/12/11 05:30')]   )

zero = int(data[0][0][1])
print(  data[10654 -zero] )   # [('ID', '10654'), ('Time', '2018/12/11 04:30')]
if your data is not sorted by id, or some records are missing, you can write some search functions like here
https://onlinegdb.com/SJkT0WR14
------
Actually you have not dictionary, but if you may change data format to dictionary, like this you can address records direct
data = { 10652: [('ID', '10652'), ('Time', '2018/12/11 02:30')],  \
         10653: [('ID', '10653'), ('Time', '2018/12/11 03:30')],  \
         10654: [('ID', '10654'), ('Time', '2018/12/11 04:30')],  \
         10655: [('ID', '10655'), ('Time', '2018/12/11 05:30')]   }

# you can address records:
print(data[10655] ) # [('ID', '10655'), ('Time', '2018/12/11 05:30')] 
Reply
#7
I merged the two threads and change the title, in order to keep discussion in one place.

In your code you have this line data = [row for row in reader]. data is list of OrderedDict objects. Now if you want to access any of these objects you need to use index, you search by id.
In order to be able to search by id change your code like this (assuming id is unique value and can be used as a key)
with open('summary.txt') as f:
    reader = csv.DictReader(f, delimiter=',')
    data = {int(row['ID']):row for row in reader}
 
print(data.get(1065, None)
Note that we convert row['ID'] to int when use it as key. That's because you used int in your example. What you get from the file is str and will be str unless you convert it to some other type.
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
Thanks, Buran

That is what I was looking for.
Gary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pathlib import not working chriswrcg 9 3,723 May-29-2022, 07:37 PM
Last Post: snippsat
  How to keep a loop containing a web import working in python executable? coder1384 3 2,887 Feb-22-2020, 06:49 PM
Last Post: snippsat
  import logging not working segerhult 3 13,228 Feb-07-2019, 05:31 PM
Last Post: buran
  import keyboard module doesn't get found - working on laptop but no on raspberry pi.. HANSJORG2 1 8,169 Mar-16-2018, 02:48 PM
Last Post: sparkz_alot
  import not working properly mepyyeti 5 4,379 Jan-18-2018, 07:08 AM
Last Post: metulburr
  import commands modules not working in python 3.6.0 bmohanraj91 2 17,644 May-01-2017, 10:59 AM
Last Post: bmohanraj91
  import antigravity not working Skaperen 8 7,461 Feb-19-2017, 05:49 AM
Last Post: Skaperen
  My import not working man_in_the_hill 4 11,440 Oct-03-2016, 10:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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