我是一名Python初學者。 所以可能是我重複相同的東西。在python中讀取一個配置文件將它存儲到一個列表中,然後將其分割成子列表
這裏問題消失 - > 我有一個名爲 installer_data.txt(包含)一個配置文件
host_ip = 10.5.5.81
services = mesos_master,hdfs_datanode,storm,kafka,zookeeper,pig
host_ip = 10.6.4.31
services = mesos_slave,zookeeper,cassandra,hdfs_namenode
我想通過這個腳本來存儲內容 - >
in_file = open("installer_data.txt","r")
lines = [line.rstrip('\n') for line in open("installer_data.txt")]
service_types =("mesos_master","mesos_slave","hdfs_namenode","hdfs_datanode","kafka","zookeeper","cassandra","pig")
service = [ f for f in lines if f in service_types]
hosts = [ f for f in lines if f not in service_types]
print service[0]
錯誤
Returns Traceback (most recent call last):
File "./file_test.py", line 13, in <module>
print service[0]
IndexError: list index out of range
由於服務列表不填充。 你們可以指點我在這裏失蹤了嗎?
你希望輸出什麼? – 2014-10-16 16:05:48
@bobd,你決定配置文件嗎?如果是的話,你可能想看看https://docs.python.org/2/library/configparser.html或https://pypi.python.org/pypi/configobj/5.0.6 – Werner 2014-10-16 16:09:28
我期待着兩個名單Vishnu ; service_type = ['mesos_master','mesos_slave','hdfs_namenode','hdfs_datanode','kafka','zookeeper','cassandra','pig']和hosts = ['10.5.5.81','10.x。 ..',..'] – bobd 2014-10-16 16:15:57