一个简单的读取修改.ini文件

1_CfgCalibration.ini

[oneTeaching]
Teachingcameraname=camera1
Teachingcameraname2=camera2
scannerpos_x=-0.035250
scannerpos_y=-0.057706
scannerpos_z=0.530346
scannerpos_a=2.962000
scannerpos_b=-0.016732
scannerpos_g=-0.280386
scannerpos2_x=-0.047764
scannerpos2_y=-0.082923
scannerpos2_z=0.641783
scannerpos2_a=2.810813
scannerpos2_b=0.151447
scannerpos2_g=-0.045385
robotPos_x=2137.998535
robotPos_y=388.199951
robotPos_z=1460.365723
robotPos_a=179.622482
robotPos_b=0.349213
robotPos_g=-177.181900
robotGrab_x=2140.600098
robotGrab_y=391.170502
robotGrab_z=1010.086182
robotGrab_a=179.609482
robotGrab_b=0.349398
robotGrab_g=-177.153351
robotGrab_x1=2137.998535
robotGrab_y1=388.199951
robotGrab_z1=1460.365723
robotGrab_a1=179.622482
robotGrab_b1=0.349213
robotGrab_g1=-177.181900
scannerpos_x1=-0.035241
scannerpos_y1=-0.057673
scannerpos_z1=0.530339
scannerpos_a1=2.961833
scannerpos_b1=-0.018006
scannerpos_g1=-0.275662
scannerpos_x2=-0.047847
scannerpos_y2=-0.082620
scannerpos_z2=0.641703
scannerpos_a2=2.810459
scannerpos_b2=0.145713
scannerpos_g2=-0.070083

[twoTeaching]
Teachingcameraname=camera1
Teachingcameraname2=camera2
scannerpos_x=-0.034173
scannerpos_y=-0.058286
scannerpos_z=0.527839
scannerpos_a=2.961884
scannerpos_b=-0.009368
scannerpos_g=-0.270069
scannerpos2_x=-0.047816
scannerpos2_y=-0.083954
scannerpos2_z=0.642303
scannerpos2_a=2.807863
scannerpos2_b=0.140559
scannerpos2_g=-0.088479
robotPos_x=823.364868
robotPos_y=-2110.255127
robotPos_z=1448.835205
robotPos_a=179.427277
robotPos_b=-0.120501
robotPos_g=92.325874
robotGrab_x=827.851074
robotGrab_y=-2109.220947
robotGrab_z=999.459839
robotGrab_a=179.427231
robotGrab_b=-0.120519
robotGrab_g=92.325874
robotGrab_x1=823.364868
robotGrab_y1=-2110.255127
robotGrab_z1=1448.835205
robotGrab_a1=179.427277
robotGrab_b1=-0.120501
robotGrab_g1=92.325874
scannerpos_x1=-0.034364
scannerpos_y1=-0.058444
scannerpos_z1=0.527883
scannerpos_a1=2.962354
scannerpos_b1=-0.010111
scannerpos_g1=-0.280445
scannerpos_x2=-0.047538
scannerpos_y2=-0.084330
scannerpos_z2=0.642297
scannerpos_a2=2.817609
scannerpos_b2=0.155142
scannerpos_g2=-0.053866

python读取、修改.ini文件

#-*- coding: utf-8 -*-

import configparser
cf = configparser.ConfigParser()
cf.read("CfgCalibration.ini")
print(cf.options('oneTeaching'))

#读取ini
x = cf.get('oneTeaching','robotgrab_x')
y = cf.get('oneTeaching','robotgrab_y')
z = cf.get('oneTeaching','robotgrab_z')
a = cf.get('oneTeaching','robotgrab_a')
b = cf.get('oneTeaching','robotgrab_b')
g = cf.get('oneTeaching','robotgrab_g')
print(x,"**",y,"**",z,"**",a,"**",b,"**",g)

#修改ini
cf.set('oneTeaching',"robotgrab_g",'-177.153351')
cf.write(open('CfgCalibration.ini','w'))

 

你可能感兴趣的:(python)