以下代碼:python
import os from utils.file_reader import YamlReader BASE_PATH = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0] CONFIG_FILE = os.path.join(BASE_PATH, 'config', 'config.yml') DATA_PATH = os.path.join(BASE_PATH, 'data') DRIVER_PATH = os.path.join(BASE_PATH, 'drivers') LOG_PATH = os.path.join(BASE_PATH, 'log') REPORT_PATH = os.path.join(BASE_PATH, 'report') class Config: def __init__(self, config = CONFIG_FILE): self.config = YamlReader(config).data def get(self, element, index=0): return self.config[index].get(element)
#coding = UTF-8 import os import yaml from xlrd import open_workbook from config.configg import CONFIG_FILE class YamlReader: def __init__(self, yamlf): if os.path.exists(yamlf): # 判斷文件是否存在,若是文件不存在,則提示文件不存在並退出 self.yamlf = yamlf else: raise FileNotFoundError('文件不存在') self._data = None @property def data(self): # _data只能在本類中調用 # 若是是第一次調用data,讀取yaml文檔,不然直接返回以前保存的數據 if not self._data: with open(self.yamlf, 'rb') as f: self._data = list(yaml.safe_load_all(f)) # load後是個generator,用list組織成列表 return self._data class SheetTypeError(Exception): pass class ExcelReader: """ 讀取excel文件中的內容。返回list。 如: excel中內容爲: | A | B | C | | A1 | B1 | C1 | | A2 | B2 | C2 | 若是 print(ExcelReader(excel, title_line=True).data),輸出結果: [{A: A1, B: B1, C:C1}, {A:A2, B:B2, C:C2}] 若是 print(ExcelReader(excel, title_line=False).data),輸出結果: [[A,B,C], [A1,B1,C1], [A2,B2,C2]] 能夠指定sheet,經過index或者name: ExcelReader(excel, sheet=2) ExcelReader(excel, sheet='BaiDuTest') """ def __init__(self, excel, sheet=0, title_line=True): if os.path.exists(excel): self.excel = excel else: raise FileNotFoundError('excel文件不存在!') self.sheet = sheet self.title_line = title_line self._data = list() @property def data(self): if not self._data: workbook = open_workbook(self.excel) if type(self.sheet) not in [int, str]: raise SheetTypeError('Please pass in <type int> or <type str>, not {0}'.format(type(self.sheet))) elif type(self.sheet) == int: s = workbook.sheet_by_index(self.sheet) if self.title_line: title = s.row_values(0) # 首行爲title for col in range(1, s.nrows): # 依次遍歷其他行,與首行組成dict,拼到self._data中 self._data.append(dict(zip(title, s.row_values(col)))) else: for col in range(0, s.nrows): # 遍歷全部行,拼到self._data中 self._data.append(s.row_values(col)) return self._data if __name__ == '__main__': y = CONFIG_FILE reader = YamlReader(y) print(reader.data) def __init__(self, excel, sheet=0, title_line=True): if os.path.exists(excel): self.excel = excel else: raise FileNotFoundError('excel文件不存在!') self.sheet = sheet self.title_line = title_line self._data = list() @property def data(self): if not self._data: workbook = open_workbook(self.excel) if type(self.sheet) not in [int, str]: raise SheetTypeError('Please pass in <type int> or <type str>, not {0}'.format(type(self.sheet))) elif type(self.sheet) == int: s = workbook.sheet_by_index(self.sheet) if self.title_line: title = s.row_values(0) # 首行爲title for col in range(1, s.nrows): # 依次遍歷其他行,與首行組成dict,拼到self._data中 self._data.append(dict(zip(title, s.row_values(col)))) else: for col in range(0, s.nrows): # 遍歷全部行,拼到self._data中 self._data.append(s.row_values(col)) return self._data if __name__ == '__main__': y = CONFIG_FILE reader = YamlReader(y) print(reader.data)
第二段代碼運行時提示ImportError: cannot import name 'CONFIG_FILE'app
在網上查了很多資料,最終肯定是由於循環導入的緣由,只要推遲進口就解決了,第二段代碼修改以下:ui
#coding = UTF-8 import os import yaml from xlrd import open_workbook class YamlReader: ... ...
if __name__ == '__main__': from config.configg import CONFIG_FILE # 由最上面導入挪到此處就能夠了 y = CONFIG_FILE reader = YamlReader(y) print(reader.data)
參考資料:https://stackoverflow.com/questions/1556387/circular-import-dependency-in-pythonspa
代碼參考:http://blog.csdn.net/huilan_same/article/details/76572466.net