Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import file into dictionary
#1
I have a file with the following. It is delimited with tabs

Quote: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

I want to import into a python dictionary name data{} and name the keys as rid, rdate, rtype, rhist, rscan.

here is what I have so far. It imports the file
import csv
with open('summary2.txt') as f:
    reader = csv.DictReader(f)
    data = [r for r in reader]

print(data)

[OrderedDict([('132\t2018/07/28 01:41\t0\t141', '133\t2018/07/28 01:56\t0\t133')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '134\t2018/07/28 02:11\t0\t126')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '135\t2018/07/28 02:27\t1\t\t123')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '137\t2018/07/28 02:27\t0\t126')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '138\t2018/07/28 02:42\t0\t119')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '139\t2018/07/28 02:57\t0\t96')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '140\t2018/07/28 03:12\t0\t79')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '141\t2018/07/28 03:27\t0\t71')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '142\t2018/07/28 03:42\t0\t69')]), OrderedDict([('132\t2018/07/28 01:41\t0\t141', '143\t2018/07/28 03:57\t0\t72')])]

How can I import the file with the predefined keys?

Thanks
Gary
Reply
#2
import csv
with open('summary2.txt') as f:
    fieldnames = [ 'rid', 'rdate', 'rtype', 'rhist', 'rscan']
    reader = csv.DictReader(f, fieldnames=fieldnames, delimiter='\t')
    data = [row for row in reader]
    print(data)
Output:
[OrderedDict([('rid', '132'), ('rdate', '2018/07/28'), ('rtype', '01:41'), ('rhi st', '0'), ('rscan', '141')]), OrderedDict([('rid', '133'), ('rdate', '2018/07/2 8'), ('rtype', '01:56'), ('rhist', '0'), ('rscan', '133')]), OrderedDict([('rid' , '134'), ('rdate', '2018/07/28'), ('rtype', '02:11'), ('rhist', '0'), ('rscan', '126')]), OrderedDict([('rid', '135'), ('rdate', '2018/07/28'), ('rtype', '02:2 7'), ('rhist', '1'), ('rscan', '123')]), OrderedDict([('rid', '137'), ('rdate', '2018/07/28'), ('rtype', '02:27'), ('rhist', '0'), ('rscan', '126')]), OrderedDi ct([('rid', '138'), ('rdate', '2018/07/28'), ('rtype', '02:42'), ('rhist', '0'), ('rscan', '119')]), OrderedDict([('rid', '139'), ('rdate', '2018/07/28'), ('rty pe', '02:57'), ('rhist', '0'), ('rscan', '96')]), OrderedDict([('rid', '140'), ( 'rdate', '2018/07/28'), ('rtype', '03:12'), ('rhist', '0'), ('rscan', '79')]), O rderedDict([('rid', '141'), ('rdate', '2018/07/28'), ('rtype', '03:27'), ('rhist ', '0'), ('rscan', '71')]), OrderedDict([('rid', '142'), ('rdate', '2018/07/28') , ('rtype', '03:42'), ('rhist', '0'), ('rscan', '69')]), OrderedDict([('rid', '1 43'), ('rdate', '2018/07/28'), ('rtype', '03:57'), ('rhist', '0'), ('rscan', '72 ')])]
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
Wow!!!!
Worked great.

Thank you - Thank you
Gary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,914 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 861 Jan-24-2023, 02:48 PM
Last Post: demdej
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 1,902 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
  trying to write a dictionary in a csv file CompleteNewb 13 6,635 Mar-04-2022, 04:43 AM
Last Post: deanhystad
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 1,861 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 3,176 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Python - Import file sequence into Media Pool jensenni 1 2,154 Feb-02-2021, 05:11 PM
Last Post: buran
Smile Import error with local file colt 1 1,954 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  saving a dictionary as json file vinay_py 6 3,117 Jun-06-2020, 05:07 PM
Last Post: vinay_py
  Code import .CSV file to MySQL table rtakle 4 2,874 Apr-30-2020, 03:16 PM
Last Post: anbu23

Forum Jump:

User Panel Messages

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