CSV import is not working - 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: CSV import is not working (/thread-14678.html) |
CSV import is not working - gehrenfeld - Dec-11-2018 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 RE: CSV import is not working - buran - Dec-11-2018 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 RE: CSV import is not working - gehrenfeld - Dec-11-2018 You were correct. Thanks for the help Print one line in dictionary - gehrenfeld - Dec-11-2018 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 RE: Print one line in dictionary - micseydel - Dec-11-2018 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. RE: Print one line in dictionary - zemiak - Dec-12-2018 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')] RE: CSV import is not working - buran - Dec-12-2018 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. RE: CSV import is not working - gehrenfeld - Dec-12-2018 Thanks, Buran That is what I was looking for. |