2017-07-26 62 views
0

我想修改一些文本文件。但是我的腳本太長了。怎樣才能使文字輸出變得輕鬆簡單?

怎麼可以做得短?誰可以建議我的劇本。

我想製作循環acap = line[4] ucap = line[5] ucp = line[6] saved = line[8]值如tcap

下面是我的腳本。

with open("find_c_volume_show.txt", "r") as f: 
    for line in f: 
     if "%" in line: 
      line = line.split(',') 
      vser = line[0] 
      vol = line[1] 
      aggr = line[2] 
      node = line[7] 

      if 'TB' in line[3]: 
       tcap = str(line[3].split("TB")[0]) 
       tcap = float(tcap) if '.' in tcap else int(tcap) 
      elif 'GB' in line[3]: 
       tcap = str(line[3].split("GB")[0]) 
       tcap = (float(tcap) if '.' in tcap else int(tcap) /1024) 
      elif 'MB' in line[3]: 
       tcap = str(line[3].split("MB")[0]) 
       tcap = (float(tcap) if '.' in tcap else int(tcap) /1024 /1024) 
      elif 'KB' in line[3]: 
       tcap = str(line[3].split("KB")[0]) 
       tcap = (float(tcap) if '.' in tcap else int(tcap) /1024 /1024 /1024) 
      elif 'B' in line[3]: 
       tcap = str(line[3].split("B")[0]) 
       tcap = (float(tcap) if '.' in tcap else int(tcap) /1024 /1024 /1024 /1024) 
      else: 
       print ("Please check script.") 

      if 'TB' in line[4]: 
       acap = str(line[4].split("TB")[0]) 
       acap = float(acap) if '.' in acap else int(tcap) 
      elif 'GB' in line[4]: 
       acap = str(line[4].split("GB")[0]) 
       acap = (float(acap) if '.' in acap else int(tcap) /1024) 
      elif 'MB' in line[4]: 
       acap = str(line[4].split("MB")[0]) 
       acap = (float(acap) if '.' in acap else int(tcap) /1024 /1024) 
      elif 'KB' in line[4]: 
       acap = str(line[4].split("KB")[0]) 
       acap = (float(acap) if '.' in acap else int(tcap) /1024 /1024 /1024) 
      elif 'B' in line[4]: 
       acap = str(line[4].split("B")[0]) 
       acap = (float(acap) if '.' in acap else int(tcap) /1024 /1024 /1024 /1024) 
      else: 
       print ("Please check script.") 

      if 'TB' in line[5]: 
       ucap = str(line[5].split("TB")[0]) 
      ..... 

      if 'TB' in line[6]: 
       ucp = str(line[6].split("TB")[0]) 

      ..... 

      if 'TB' in line[8]: 
       saved = str(line[8].split("TB")[0]) 

      ..... 
      line = vser, vol, aggr, tcap, acap, ucap, ucp, saved 
    print (line) 

我想在文本中匹配TB容量格式。下面是 是我的文本文件。

Vserver,Volume,Aggregate,Total,Avail,Used,UsedP,Node,Saved 
FAS8040-ZZZZ,vol0,Node1_aggr0,466.6GB,436.0KB,30.60MB,6%,FAS8040-ZZZZ,0B 
FAS8040-YYYY,vol0,Node2_aggr0,46.6GB,428.6KB,38.07B,8%,FAS8040-YYYY,23KB 
FAS8040-XXXX,vol0,Node1_aggr0,466GB,436.0GB,30.60GB,6%,FAS8040-XXXX,44GB 
FAS8040-AAAA,vol0,Node2_aggr0,466.6TB,428.6MB,38.07MB,8%,FAS8040-AAAA,102MB 
+1

'elif的 'GB':',你的意思'elif的 'GB' 本着[3]:'? – cdarke

+1

太長了?這是30行,這是什麼問題? – JulienD

+0

當你說你的腳本太長,你的意思是太多的代碼行,或者你的意思是它太慢? – cdarke

回答

1

也許這將幫助你開始:

unit = ['TB', 'GB', 'MB', 'KB', 'B'] 
with open("find_c_volume_show.txt", "r") as f: 
    for line in f: 
     if "%" in line: 
      line = line.split(',') 
      out = [] 
      for l in line[3:6] + [line[8]]: 
      try: 
       ind = [unit[i] in l for i in range(5)].index(True) 
      except ValueError: 
       print('please check script') 

      val = str(l.split(unit[ind])[0]) 
      out.append(float(val)/1024**ind) 

      line = line[0:3] + out[:-1] + line[6:8] + [out[-1]] 
      print(line) 
+0

非常感謝。但我無法取得成果。該腳本不起作用。請解釋你的腳本。我不明白'line = line [0:3] + out [: - 1] + line [7] + out [-1]' – KJ9

+0

嗨,是的,我做了一些更改,這應該會更好。您提到的這一行只是將多個列表連接成一個列表。 'list'的python中的'+'運算符只是執行連接。請注意,這行代碼有錯誤,我在更新的答案中更正了它。 –

+0

太棒了!非常感謝。 – KJ9

相關問題