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


Messages In This Thread
import file into dictionary - by gehrenfeld - Dec-11-2018, 02:42 PM
RE: import file into dictionary - by buran - Dec-11-2018, 02:53 PM
RE: import file into dictionary - by gehrenfeld - Dec-11-2018, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 3,149 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 922 Jan-24-2023, 02:48 PM
Last Post: demdej
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 2,012 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
  trying to write a dictionary in a csv file CompleteNewb 13 6,924 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,929 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 3,348 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Python - Import file sequence into Media Pool jensenni 1 2,235 Feb-02-2021, 05:11 PM
Last Post: buran
Smile Import error with local file colt 1 2,011 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  saving a dictionary as json file vinay_py 6 3,252 Jun-06-2020, 05:07 PM
Last Post: vinay_py
  Code import .CSV file to MySQL table rtakle 4 3,016 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