0
大家早上好! (是的,我知道,這是沒有必要在你身邊的早晨;-))YAML加載期間的python str錯誤
我來找你,因爲我開始我的一天,一段代碼,我無法解釋一個小錯誤。
我目前正在建設,其目的是做兩件簡單的事情類:
打開YAML格式的配置文件,然後加載此一個。
對我來說不幸的是,我的全新革命計劃(顯然是一個笑話)不想今天成爲一名好戰士,並且拒絕做它應該做的事情。
這裏是我的代碼:
的main.py部分:
#!/usr/bin/env python
import socket
from models import setting
run = setting.app_config()
run.get_config()
,這裏是設置模塊:
import os
import yaml
class app_config:
def __init__(self, custom_cfg=None):
self.workdir = os.getcwd()
self.default_config_name = 'app_config.yaml'
self.default_config_path = self.workdir+"/assets/"
if custom_cfg is None:
self.config_file_path = self.default_config_path+self.default_config_name
else:
self.config_file_path = custom_cfg
def get_config(self):
app_config_file = open(self.config_file_path, 'r')
parsed_config_file = yaml.load(app_config_file, 'r')
app_config_file.close()
network_settings = parsed_config_file['network']
hostname = network_settings['host']
socket = network_settings['port']
buffer_size = network_settings['buffer_size']
print network_settings, hostname, socket, buffer_size
所以,現在,與那些兩件問題代碼似乎來自此聲明:
parsed_config_file = yaml.load(app_config_file, 'r') <-- Line 22
這導致一個非常美麗的錯誤消息:
Traceback (most recent call last):
File "main.py", line 6, in <module>
run.get_config()
File "/home/dri/devil_project/models/setting.py", line 22, in get_config
parsed_config_file = yaml.load(app_config_file, 'r')
File "/usr/local/lib/python2.6/dist-packages/yaml/__init__.py", line 69, in load
loader = Loader(stream)
TypeError: 'str' object is not callable
現在,如果我嘗試打印我的app_config_file變量或試圖使用一個for循環,一切都是完美的,打印工作正常讀取這個文件,我的文件打開,我可以閱讀它。
嗯,如果有人使用YAML模塊(PyYAML一個)已經遇到這種情況,我會有興趣通過他的解決方案;-)
非常感謝!
好吧,好吧,我明白了,那是把戲,我想解析文件whitch是指向我的app_config_file變量。 實際上,config_file_path變量只是一個動態字符串對象,它告訴程序在哪裏查看。 非常感謝你讓我的一天;-) –