Hi, I'm struggling here to do the following with any success:
I have a comma delimited file where each line in the file is something
like:
PNumber,3056,Co ntractor,XYZ Contracting,Arc hitect,ABC Architects,...
So each line is intended to be: key1,value1,key 2,value2,key3,v alue3...
and each line is to be variable in length (although it will have to be
an even number of records so that each key has a value).
I want to read in this csv file and parse it into a list of
dictionaries. So each record in the list is a dictionary:
{"PNumber":"305 6","Contractor" :"XYZ Contracting", ... }
I have no problem reading in the CSV file to a list and splitting each
line in the file into its comma separated values. But I can't figure
out how to parse each resulting list into a dictionary.
Any help on this? 7 7659
RFQ wrote: Hi, I'm struggling here to do the following with any success:
I have a comma delimited file where each line in the file is something like:
PNumber,3056,Co ntractor,XYZ Contracting,Arc hitect,ABC Architects,...
So each line is intended to be: key1,value1,key 2,value2,key3,v alue3... and each line is to be variable in length (although it will have to be an even number of records so that each key has a value).
I want to read in this csv file and parse it into a list of dictionaries. So each record in the list is a dictionary:
{"PNumber":"305 6","Contractor" :"XYZ Contracting", ... }
I have no problem reading in the CSV file to a list and splitting each line in the file into its comma separated values. But I can't figure out how to parse each resulting list into a dictionary.
First, don't process the CSV stuff yourself. Use the csv module.
In [9]:import csv
In [10]:f = open('foo.csv')
In [11]:cr = csv.reader(f)
In [12]:for row in cr:
....: print dict(zip(row[::2], row[1::2]))
....:
{'Architect': 'ABC Architects', 'PNumber': '3056', 'Contractor': 'XYZ
Contracting'}
{'Architect': 'ABC Architects', 'PNumber': '3056', 'Contractor': 'XYZ
Contracting'}
[etc.]
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
RFQ wrote: I have a comma delimited file where each line in the file is something like:
PNumber,3056,Co ntractor,XYZ Contracting,Arc hitect,ABC Architects,...
So each line is intended to be: key1,value1,key 2,value2,key3,v alue3... and each line is to be variable in length (although it will have to be an even number of records so that each key has a value).
I want to read in this csv file and parse it into a list of dictionaries. So each record in the list is a dictionary:
{"PNumber":"305 6","Contractor" :"XYZ Contracting", ... } row
['PNumber', '3056', 'Contractor', 'XYZ Contracting', 'Architect', 'ABC'] dict(zip(row[::2], row[1::2]))
{'Architect': 'ABC', 'PNumber': '3056', 'Contractor': 'XYZ Contracting'}
A bit more elegant:
irow = iter(row) dict(zip(irow, irow))
{'Architect': 'ABC', 'PNumber': '3056', 'Contractor': 'XYZ Contracting'}
Peter
Sounds like you want to use the ConfigObject module. http://www.voidspace.org.uk/python/m...html#configobj
-dave
"RFQ" <rf******@gmail .com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com... Hi, I'm struggling here to do the following with any success:
I have a comma delimited file where each line in the file is something like:
PNumber,3056,Co ntractor,XYZ Contracting,Arc hitect,ABC Architects,...
So each line is intended to be: key1,value1,key 2,value2,key3,v alue3... and each line is to be variable in length (although it will have to be an even number of records so that each key has a value).
I want to read in this csv file and parse it into a list of dictionaries. So each record in the list is a dictionary:
{"PNumber":"305 6","Contractor" :"XYZ Contracting", ... }
I have no problem reading in the CSV file to a list and splitting each line in the file into its comma separated values. But I can't figure out how to parse each resulting list into a dictionary.
Any help on this?
-- http://mail.python.org/mailman/listinfo/python-list
RFQ wrote: Hi, I'm struggling here to do the following with any success:
I have a comma delimited file where each line in the file is something like:
PNumber,3056,Co ntractor,XYZ Contracting,Arc hitect,ABC Architects,...
This is NOT a CSV file. A CSV file would be :
PNumber,Contrac tor,Architect,. ..
2056,XYZ Contracting,ABC Architects,...
Then, you could use the built-in CSV module of recent python versions. So each line is intended to be: key1,value1,key 2,value2,key3,v alue3... and each line is to be variable in length (although it will have to be an even number of records so that each key has a value).
I want to read in this csv file and parse it into a list of dictionaries. So each record in the list is a dictionary:
{"PNumber":"305 6","Contractor" :"XYZ Contracting", ... }
I have no problem reading in the CSV file to a list and splitting each line in the file into its comma separated values. But I can't figure out how to parse each resulting list into a dictionary.
Any help on this?
By,
Laurent RAHUEL wrote: RFQ wrote:
Hi, I'm struggling here to do the following with any success:
I have a comma delimited file where each line in the file is something like:
PNumber,3056, Contractor,XYZ Contracting,Arc hitect,ABC Architects,...
This is NOT a CSV file. A CSV file would be :
PNumber,Contrac tor,Architect,. .. 2056,XYZ Contracting,ABC Architects,...
CSV is an acronym for "Comma-Separated Values". It does not imply
anything about the contents of the fields. The OP's file *is* a CSV
file. Yes, the contents do represent an unusual application of the CSV
format -- however a bus full of parcels instead of people is still a bus.
Then, you could use the built-in CSV module of recent python versions.
Python is a case-sensitive language. The name of the module is "csv".
The OP could use the csv module with his data.
John Machin wrote: Laurent RAHUEL wrote: RFQ wrote:
Hi, I'm struggling here to do the following with any success:
I have a comma delimited file where each line in the file is something like:
PNumber,3056 ,Contractor,XYZ Contracting,Arc hitect,ABC Architects,...
This is NOT a CSV file. A CSV file would be :
PNumber,Contrac tor,Architect,. .. 2056,XYZ Contracting,ABC Architects,...
CSV is an acronym for "Comma-Separated Values". It does not imply anything about the contents of the fields. The OP's file *is* a CSV file. Yes, the contents do represent an unusual application of the CSV format -- however a bus full of parcels instead of people is still a bus.
I thought you knew the number of cols and what you should expect in each.
Then it sounded pretty easy to build a list of dictionaries. If you don't
know what you're supposed to find in your file and how this file is
structured I guess you don't know what you are doing. Then, you could use the built-in CSV module of recent python versions.
Python is a case-sensitive language. The name of the module is "csv". The OP could use the csv module with his data.
Damn, that's why I always see those annoynig import errors.
I just wanted to help, maybe you're to much case-sensitive.
Regards,
Laurent.
Laurent RAHUEL wrote: I thought you knew the number of cols and what you should expect in each. Then it sounded pretty easy to build a list of dictionaries. If you don't know what you're supposed to find in your file and how this file is structured I guess you don't know what you are doing.
That's not what the OP asked about.
[RFQ:]
"""So each line is intended to be: key1,value1,key 2,value2,key3,v alue3...
and each line is to be variable in length (although it will have to be
an even number of records so that each key has a value)."""
The rows are not all of the same format. The OP *does* know the
structure, and he (?) *does* know what he's doing. It's just not the
structure usually used in CSV files.
The csv module, of course, still reads these rows just fine; they just
need to be processed a bit to get the correct dictionaries.
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Luis P. Mendes |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
my program builds a dictionary that I would like to save in a file.
My question is what are the simple ways to do it?
The first solution I've thought of is to transform the dictionary in a
|
by: Philipp H. Mohr |
last post by:
Hello,
I would like to store multiple dictionaries in a file, if possible one per
line. My code currently produces a new dictionary every iteration and
passes it on to another peace of code. In order to be able to re-run some
experiments at a later date I would like to store every dictionary in the
same file.
I looked at pickel, but that seems to require a whole file for each
dictionary.
|
by: Angelic Devil |
last post by:
I'm building a file parser but I have a problem I'm not sure how to
solve. The files this will parse have the potential to be huge
(multiple GBs). There are distinct sections of the file that I
want to read into separate dictionaries to perform different
operations on. Each section has specific begin and end statements
like the following:
KEYWORD
..
..
|
by: Christoph Zwerschke |
last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist
only since Python 2.4.
Anyway, I was thinking about whether it would be possible and desirable
to change the old behavior in future Python versions and let dict.keys()
and dict.values() both return sets instead of lists.
If d is a dict, code like:
for x in d.keys():
|
by: David Pratt |
last post by:
Hi. I like working with lists of dictionaries since order is preserved
in a list when I want order and the dictionaries make it explicit what
I have got inside them. I find this combination very useful for storing
constants especially. Generally I find myself either needing to
retrieve the values of constants in an iterative way (as in my
contrived example below). Perhaps even more frequent is given one value
is to look up the matching...
| |
by: rbt |
last post by:
What's a good way to write a dictionary out to a file so that it can be
easily read back into a dict later? I've used realines() to read text
files into lists... how can I do the same thing with dicts? Here's some
sample output that I'd like to write to file and then read back into a dict:
{'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994,
'.\\New Text Document.txt': 1135900552}
|
by: techiepundit |
last post by:
I'm a Python newbie who just started learning the language a few weeks
ago. So these are beginner questions.
I have a list of sockets that I use for select.select calls like this:
ReadList,WriteList,EventList = select.select(self.SocketList,,,3)
In parallel with that list of sockets I want something that is like a
list of socket,PacketFragment pairs. I need this because I could do
recv and get part of a sent packet. I want to loop...
|
by: shigehiro |
last post by:
Hi all,
Further to my 'list of dictionaries' question yesterday, I would to ask how to write data to csv file format using 'list of dictionaries' as a source:
data to be written to .csv file:
dictList =
so the csv file would look something like:
Michael,Kirk,224567
Linda,Matthew,123456
|
by: Jon Bowlas |
last post by:
Many thanks for all your reponses, much appreciated.
I'll get back to you on which is the best for me.
BTW - yes John thats exactly what I wanted.
Cheers
Jon
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |