configparser python_configparser 在python3下的简单使用

1.创建configparser文件

import configparser

#导入模块

config = configparser.ConfigParser() #注意大小写与()

config['DEFAULT'] = {'Server': '45', 'Compression': 'yes'}

config['server'] = {'deletehq':'0',

'localtime':'20180706',

'port':'22'}

config['system'] = {'market64':'xiadan1.exe',

'market128':'xiadan2.exe',

'market256':'xiadan3.exe' }

config['client'] = {}

with open('configTest.ini', 'w') as configfile:

config.write(configfile)

或者通过字典创建

config = configparser.ConfigParser()

config.read_dict( {

'section1': {'key1': 'value1',

'key2': 'value2',

'key3': 'value3'},

'section2': {'keyA': 'valueA',

'keyB': 'valueB',

'keyC': 'valueC'},

'section3': {'foo': 'x',

'bar': 'y',

'fun: 'z'}

} )

with open('configTest1.ini', 'w') as configfile:

config.write(configfile)

2.读取配置文件

config.read('configTest.ini')

[DEFAULT]

server = 45

compression = yes

[server]

deletehq = 0

localtime = 20180706

port = 22

[system]

market64 = xiadan1.exe

market128 = xiadan2.exe

market256 = xiadan3.exe

[client]

3.读取操作

获取所有sections

print(config.sections())

['server', 'system', 'client']

获取指定 section 的 keys & values

print(config.items('system'))

[('server', '45'), ('compression', 'yes'), ('market64', 'xiadan1.exe'), ('market128', 'xiadan2.exe'), ('market256', 'xiadan3.exe')] # 注意items()返回的字符串会全变成小写

获取指定 section 的 keys

print(config.options('system'))

['market64', 'market128', 'market256', 'server', 'compression'] #会打印default中的keys

获取指定 key 的 value

print(config['system']['market64'])

xiadan1.exe

4.检查

‘section’ in config

‘option’ in config['section']

config.has_section['section']

config.hais_option['section','option']

5.添加

config.add_section('section4')

config.set('section4','key1','value1')

config.write(open('configTest1.ini','w')) #写入

[section4]

key1 = value1

6.删除

config.remove_option('section4','key1') #删除option

config.remove_section('section4') #删除section

7.[DEFAULT]

[DEFAULT] 一般包含 ini 格式配置文件的默认项,所以 configparser 部分方法会自动跳过这个 section 。 sections() 是获取不到的,还有删除方法对 [DEFAULT] 也无效,但指定删除和修改 [DEFAULT] 里的 keys & values 是可以的,还有个特殊的是,has_section() 也无效,可以和 in 区别使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标用户群:习惯用通达信看盘,或习惯用大智慧看盘,但又喜欢用同花顺单独交易程序(xiadan.exe)来交易。 配置方式:打开压缩包中的配置文件,配置 "下单程序"的路径属性,即将path后面的路径改成你本机xiadan.exe的真实路径。(务必保持配置文件和本脚本名字一致) 使用步骤: 1、配置好上面说的路径 2、启动看盘软件,如通达信 3、启动本工具 4、本工具会根据上面所配置路径启动xiadan.exe 5、手动登录xiadan.exe 6、程序会发送F1、F2检查下单功能 7、无误后会在屏幕上方看到下单助手工具条。(点住左侧的矩形可以移动工具条的位置) 8、在通达信(大智慧)上浏览某股票,会同步更新到工具条上。 9、点击买或卖单选框可以切换到买模式或卖模式。(展示相应按钮,屏蔽无关按钮,避免操作出错。核买按钮就是涨停买,核卖就是跌停卖。) 10、选择仓位或者输入手数 11、选择价格或输入价格 12、点击下单按钮 13、本程序会将:股票代码、下单数量、下单价格、自动填写到 xiadan.exe 中。(并会核算下单数量是否符合可下单额度) 14、如选择了闪电复选框,那么在上面第13步下单确认窗口会自动被点击确认。(初期测试不建议选择闪电,另请务必在模拟账户上进行测试,本脚本只做为学习交流,不建议实盘应用,稳定性尚需打磨,欢迎交流探讨。)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值