[英]Unable to get key from ConfigParser value
I create.txt file inside the same folder to store Api Key, user name, etc... as you can see in Image 1我在同一个文件夹中创建了.txt 文件来存储 Api 密钥、用户名等...如图 1 所示
in Image 2 you will find how I enter the info (Using = and no '')在图 2 中,您会发现我是如何输入信息的(使用 = 而没有 '')
With this code I´m trying to import data from the.txt file into the code (Image 3):使用此代码,我尝试将 .txt 文件中的数据导入代码(图 3):
config = configparser.ConfigParser() config.read("config.ini") api_id = config['Telegram']['api_id'] api_hash = config['Telegram']['api_hash'] api_hash = str(api_hash) phone = config['Telegram']['phone'] username = config['Telegram']['username']
And I´m getting the next error as you can see in Image 3.正如您在图 3 中看到的那样,我遇到了下一个错误。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-31-26acbcbbad1b> in <module>
4
5 # Setting configuration values
----> 6 api_id = config['Telegram']['api_id']
7 api_hash = config['Telegram']['api_hash']
8
/opt/anaconda3/lib/python3.7/configparser.py in __getitem__(self, key)
956 def __getitem__(self, key):
957 if key != self.default_section and not self.has_section(key):
--> 958 raise KeyError(key)
959 return self._proxies[key]
960
KeyError: 'Telegram'
What I´m doing wrong?我做错了什么?
You are giving wrong path in config.read()
.您在
config.read()
中给出了错误的路径。
Try absolute path, It should work尝试绝对路径,它应该工作
config = configparser.ConfigParser()
config.read("/path/to/config.ini")
Use the actual path, which you can get from right click on this file.使用实际路径,您可以通过右键单击此文件获得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.