我正在尝试将数据库名称分隔为一个名为/ etc / oratab的文件:
#Commends
DBNAME1:/opt/oracle/product/19000/db:N
DBNAME2:/opt/oracle/product/19000/db:N
以下代码仅返回DBNAME1:
def oratab():
dblist=[]
flag=False
with open('/etc/oratab', 'r') as oratab:
for line in oratab:
if line and line[0].isalpha():
flag=True
if flag:
dblist.append(line)
return ''.join(dblist).split(':')[0]
如何更改脚本以获得DBName的完整列表?
最佳答案
您的dblist
是lines
的列表,并且您要从split
行而不是dblist
返回第一个元素。
','.join(dbline.split(':')[0] for bdline in dblist)
我认为您需要在列表中返回
list
的database names
删除
join
并返回return dblist
更好:
def oratab():
dblist=[]
with open('/etc/oratab', 'r') as oratab:
for line in oratab:
if line and line[0].isalpha():
dblist.append(line.split(':')[0])
return ','.join(dblist)
或更短
def oratab():
dblist = []
with open('/etc/oratab', 'r') as oratab:
dblist = [line.split(':')[0] in oratab if line and line[0].isalpha()]
return ','.join(dblist)
关于python - 如何从/etc/oratab文件中提取字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57818313/