初學者問題
我的文件看起來像 - >切「=」分隔的字符串的右側,並存儲到一個列表蟒蛇
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
我怎樣才能切斷IP的一部分,並將其存儲進入進一步處理的列表? 請幫忙。
初學者問題
我的文件看起來像 - >切「=」分隔的字符串的右側,並存儲到一個列表蟒蛇
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
我怎樣才能切斷IP的一部分,並將其存儲進入進一步處理的列表? 請幫忙。
answer = []
with open('path/to/file') as infile:
for line in infile:
answer.append(line.partition('=')[0].strip())
和'str.partition()'是快於有限的'str.split()'... – 2014-10-27 16:34:14
感謝您的快速響應 – bobd 2014-10-27 16:46:49
有一個看看'str.split'功能https://docs.python.org/3.4/library/stdtypes.html#str.split :) – Dettorer 2014-10-27 16:32:43