473,769 Members | 7,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ConfigParser: writes a list but reads a string?

It looks like ConfigParser will accept a list to be writing to the
*.ini file; but when reading it back in, it treats it as a string.

Example:

############### ############### #
import ConfigParser
def whatzit(thingna me, thing):
print thingname, "value:", thing
print thingname, "length:", len(thing)
print thingname, type(thing)

cfgfile = "cfgtest.in i"
config1 = ConfigParser.Co nfigParser()
config1.add_sec tion("test")

t1 = range(1,11)
config1.set("te st", "testlist", t1)
outfile=open(cf gfile,"w")
config1.write(o utfile)
outfile.close()

config2 = ConfigParser.Co nfigParser()
config2.read(cf gfile)
t2 = config2.get("te st","testlist ")

whatzit("t1", t1)
whatzit("t2", t2)

############### ############### #

Output is:

t1 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t1 length: 10
t1 <type 'list'>
t2 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t2 length: 31
t2 <type 'str'>

That is, t1 is a list of length 10, consisting of:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and is written out; but t2 is read back in as a string
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
of length 31.

It took me a while to figure this out, since they looked identical in
print statements.

Is there a pythonic way to read in a list from a .INI file with
ConfigParser? Is this expected behavior for ConfigParser? I would
not expect this conversion; rather, an exception when trying to write
the list if the list is not supported.

Jan 16 '06
10 11866

Jeffrey Barish wrote:
funkyj wrote:
making the config file XML and using xml.dom is another option,
although XML is a bit ugly to edit by hand.
--jfc
I am seriously intrigued by ConfigObj. I am currently using an crude
improvisation involving tab-delimited fields to store metadata for
recordings -- not configuration data -- in text files. I had been planning
to convert to XML, but now I am wondering whether ConfigObj would be
easier. I would like for the metadata files to be editable, but editing
does not have to be easy as it needs to be done rarely. I've never used
XML, so I am wondering whether there are other tradeoffs between that
approach and ConfigObj that I should be aware of. I was thinking of XML
mainly to have a more robust format. For example, it would be nice if it
were possible to add fields without obsoleting early versions of the
reader. Crossplatform compatibility is also desirable.


ConfigObj can be used for all sorts of data persistence type uses.
Because it is pure python there are likely to be no cross platform
issues.

You would have to 'name' entries, but entries can be single values or
lists - or even subsections (effectively dictionaries).

If you are happy with text, then it is *very* easy to use. If you
wanted to store other datatypes, then you may be interested in
ConfigPersist.p y :
http://www.voidspace.org.uk/python/a...sistence.shtml

It is easy to use a validating configspec, and then extend it - without
obsoleting earlier code or data files.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
--
Jeffrey Barish


Jan 23 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2528
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
2
5076
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
4
9119
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). I know that this can be achieved using strtok. But I was wondering
3
8494
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
9
4446
by: incredible | last post by:
how to sort link list of string
2
1429
by: Gandalf | last post by:
hi, there is some function to verify that a string is already in the list or i have to make a cicle? thank u
4
13206
by: shapper | last post by:
Hello, How can I transform a Generic List(Of String) to a string as follows: "value1,value2,value3,value4, ..." Thanks, Miguel
12
4389
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
1
3770
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void addString (const std::string s); void print_strings () const;
9
2795
by: shapper | last post by:
Hello, How can I filter a List(Of String)? I need to get the list elements which start with the letters contained in the variable Text. Thanks, Miguel
0
9423
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,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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,...
0
8873
agi2029
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5309
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.