configparser

How to remove spaces while writing in INI file- Python

我们两清 提交于 2019-12-19 20:44:43
问题 I am using a file and i have one section named DIR in which it contain the paths. EX: [DIR] DirTo=D:\Ashish\Jab Tak hai Jaan DirBackup = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Backup ErrorDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Error CombinerDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Combiner DirFrom=D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\In PidFileDIR = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Pid LogDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Log TempDir = D:\Parser\ERICSSON_CSCORE

How to remove spaces while writing in INI file- Python

女生的网名这么多〃 提交于 2019-12-19 20:44:07
问题 I am using a file and i have one section named DIR in which it contain the paths. EX: [DIR] DirTo=D:\Ashish\Jab Tak hai Jaan DirBackup = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Backup ErrorDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Error CombinerDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Combiner DirFrom=D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\In PidFileDIR = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Pid LogDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Log TempDir = D:\Parser\ERICSSON_CSCORE

ConfigParser with Unicode items

ぐ巨炮叔叔 提交于 2019-12-18 12:49:19
问题 my troubles with ConfigParser continue. It seems it doesn't support Unicode very well. The config file is indeed saved as UTF-8, but when ConfigParser reads it it seems to be encoded into something else. I assumed it was latin-1 and I thougt overriding optionxform could help: -- configfile.cfg -- [rules] Häjsan = 3 ☃ = my snowman -- myapp.py -- # -*- coding: utf-8 -*- import ConfigParser def _optionxform(s): try: newstr = s.decode('latin-1') newstr = newstr.encode('utf-8') return newstr

Python ConfigParser module rename a section

女生的网名这么多〃 提交于 2019-12-18 09:54:07
问题 How can you rename a section in a ConfigParser object? 回答1: As far as I can tell, you need to get the sections items via ConfigParser.items remove the section via ConfigParser.remove_section create a new section via ConfigParser.add_section Put the items back into the new section via ConfigParser.set 回答2: example helper function - silly really but it might save someone a few minutes work... def rename_section(cp, section_from, section_to): items = cp.items(section_from) cp.add_section(section

ConfigParser getting value from INI file with section and subsection as shown below

拟墨画扇 提交于 2019-12-12 17:34:13
问题 I have a following type of INI file [section1][subsection1] port=989 [section1][subsection2] somethign=somethign I am using ConfigParser of Python to parse the INI file but I am not able to figure it out on how to get the data from the above kinda INI file. the below code is for getting the value when INI file is like [section1] port=908 [section2] ss=ss config = ConfigParser.RawConfigParser() config.read(INI_File) mIp = config.get('section1','port') Please don't suggest me to change the INI

Argparse: defaults from file

走远了吗. 提交于 2019-12-12 10:15:04
问题 I have a Python script which takes a lot of arguments. I currently use a configuration.ini file (read using configparser ), but would like to allow the user to override specific arguments using command line. If I'd only have had two arguments I'd have used something like: if not arg1: arg1 = config[section]['arg1'] But I don't want to do that for 30 arguments. Any easy way to take optional arguments from cmd line, and default to the config file? 回答1: Try the following, using dict.update():

Rename config.ini section using ConfigParser in python

心不动则不痛 提交于 2019-12-12 01:32:25
问题 Is there an easy way to rename a section in a config file using ConfigParser in python? I'd prefer not to have to delete the section and recreate it, but that is my only answer right now. 回答1: No. The builtin ConfigParser stores sections as _sections , which is a dict. Since this is python you could access that variable to do an easy copy. ( config._sections[new_name] = config._sections[old_name]; config._sections.pop(old_name) But ConfigParser may change at some later date and this would

Reading a fortigate configuration file with Python

隐身守侯 提交于 2019-12-11 12:43:42
问题 Appologies for the really long drawn out question. I am trying to read in a config file and get a list of rules out. I have tried to use ConfigParser to do this but it is not a standard config file. The file contains no section header and no token. i.e. config section a set something to something else config subsection a set this to that next end config firewall policy edit 76 set srcintf "There" set dstintf "Here" set srcaddr "all" set dstaddr "all" set action accept set schedule "always"

ConfigParser and section with values without keys

偶尔善良 提交于 2019-12-11 11:59:38
问题 I'm just wondering. Is there any chance to create section in *.ini file to store only values without keys? I'm to store list of used ports in localhost and other servers and my list looks like this: [servers] localhost:1111 localhost:2222 localhost:3333 someserver:2222 someserver:3333 For now python treats server name as a key and port as value. But worst thing is that calling print config.items('servers') Returns me only this: localhost:3333 someserver:3333 which is wrong, but I could handle

Define order of config.ini entries when writing to file with configparser?

孤街浪徒 提交于 2019-12-11 10:32:22
问题 I'm using the Python configparser to generate config.ini files to store my scripts configuration. The config is generated by code, but the point of the file is, to have an external way to change the programmatically generated config at later stages. So the file needs to be well readable and config options should be easy to find. The sections in configparser are a nice way to ensure that, however within a section, the entries seem to be ordered randomly. For example this code: import