python3.7对应configparser
时间: 2023-08-04 15:07:39 浏览: 58
在Python 3.7中,configparser模块仍然可用,可以用于读取和写入INI格式的配置文件。你可以通过以下步骤使用configparser:
1. 导入configparser模块:
```python
import configparser
```
2. 创建一个ConfigParser对象:
```python
config = configparser.ConfigParser()
```
3. 读取和解析配置文件:
```python
config.read('config.ini')
```
这里的'config.ini'是你要读取的配置文件的文件名。
4. 获取配置文件中的值:
```python
value = config.get(section, option)
```
这里的section是配置文件中的节名,而option是节中的选项名。
5. 修改配置文件中的值:
```python
config.set(section, option, value)
```
这会将指定的值写入到配置文件中的对应位置。
6. 保存更改后的配置文件:
```python
with open('config.ini', 'w') as configfile:
config.write(configfile)
```
这将把更改后的配置写回到原始的配置文件中。
请注意,上述步骤仅为一般用法示例,具体的使用方法可以根据你的配置文件结构和需求进行调整。
相关问题
Traceback (most recent call last): File "3.py", line 131, in <module> name = config.get('Variables', 'name') File "/usr/local/python3/lib/python3.7/configparser.py", line 780, in get d = self._unify_values(section, vars) File "/usr/local/python3/lib/python3.7/configparser.py", line 1146, in _unify_values raise NoSectionError(section) from None configparser.NoSectionError: No section: 'Variables'
这个错误是由于配置文件中的节(section)名称不正确导致的。根据你的错误信息,配置文件中找不到名为`Variables`的节。
请确保你的配置文件(`test.ini`)的格式正确,并包含一个名为`Variables`的节。示例配置文件的正确格式如下:
```
[Variables]
name = 张三
age = 18
```
请检查你的配置文件,确保节的名称和配置项的格式正确。如果问题仍然存在,请提供你的配置文件内容以供进一步排查。
[root@test-01 test]# /usr/local/bin/python3 /root/test/3.py --config=/root/test/test.ini Traceback (most recent call last): File "/root/test/3.py", line 41, in <module> port = config.getint('mysql_config', 'port') File "/usr/local/python3/lib/python3.7/configparser.py", line 818, in getint fallback=fallback, **kwargs) File "/usr/local/python3/lib/python3.7/configparser.py", line 808, in _get_conv **kwargs) File "/usr/local/python3/lib/python3.7/configparser.py", line 802, in _get return conv(self.get(section, option, **kwargs)) ValueError: invalid literal for int() with base 10: '3306 # 备份库端口'
根据你提供的错误信息,`ValueError: invalid literal for int() with base 10: '3306 # 备份库端口'` 表示在解析配置文件时,尝试将字符串 `'3306 # 备份库端口'` 转换为整数时出错。
这个错误通常发生在使用 `configparser` 解析配置文件时,配置文件中的值与所期望的数据类型不匹配。
根据错误信息,问题可能出现在 'mysql_config' 节中的 'port' 键对应的值。请检查配置文件中 'port' 键对应的值是否是一个数字,并且没有多余的空格或注释。
如果值确实是一个数字,但仍然出现此错误,请提供更详细的配置文件内容和代码,以便进一步排查。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)