2017-01-27 18 views
2

は**私はctlファイルのテキストデータを水平にpythonを使って逆にして、新しいファイルに保存するには?

mycnf_001/mycnf_001_001 mycnf_001/mycnf_001_002 下に示すように、CTLファイルがテキストで構成されてい....................... .......................(1000行で構成された非常に長いリスト)

私は懸命に努力していますが、私は所望のフォーマット

mycnf_001_001 mycnf_001 mycnf_001_002 mycnf_001 ....................... .............. .........(1000行で構成された非常に長いリスト)

*********詳細な問題の説明***************************

カレントフォーマットmycnf_001/mycnf_001_001

DESIRED FROMAT mycnf_001_001 mycnf_001

//////////// CODE /////////////////////

f = open("ms.ctl", "rb") 
s = f.readlines() 
f.close() 
f = open("newms.ctl", "wb") 
s.reverse() 
for item in s: 
    print>>f, item 
f.close() 

上記のコードを実行すると、単に下から上に順番が逆になり、必要なのは明らかです上記のイオン。

+0

STP-1カレントフォーマットmycnf_001/mycnf_001_001 STP-2単にスラッシュ(/)をREFとTXTデータをねじるmycnf_001_001/mycnf_001 STPに"\n"" "を置き換えます-3を入力し、次にdel(/)でspcをb/wでtxtにします。 mycnf_001_001 mycnf_001 – Andy

答えて

0

がファイルを読み込んだ後に想定する「ms.ctlを」ことができます、あなたは今、今、あなたは文字列の配列を持っている

spliteds = [x for x in s.split('/') if x.strip()] 

「/」に対する文字列を分割

s="mycnf_001/mycnf_001_001/mycnf_001_002" 

のような値を持っています。今度は配列にアクセスして最後から別の文字列に保存してみます。

desireds="" 
for i in reversed(spliteds): 
     desireds = desireds + i + " " 

ここで、この文字列を任意のファイルに入れることができます。

CODE:

f = open("ms.ctl", "rb") 
    s = f.read() 
    f.close() 

    N = s.split('\n') 

    f = open("newms.ctl", "wb") 

    spliteds = [ x for x in N[0].split('/') if x.strip()] 
    desireds="" 
    for i in reversed(spliteds): 
      desireds = desireds + i + " " 
    #print desireds 

    f.write(desireds) 
    f.close() 

あなたは印刷を使用してこのコードをデバッグすることができます。それが役に立てば幸い。

UPDATE

あなたは、複数の行のためにそれを実行したい場合。

f = open("ms.ctl", "rb") 
s = f.read() 
f.close() 

N = s.split('\n') 
lenth = len(N) 

f = open("newms.ctl", "wb") 

for x in range(0, lenth): 
     print "We're on time %d" % (x) 
     spliteds = [ x for x in N[0].split('/') if x.strip()] 
     desireds="" 
     for i in reversed(spliteds): 
       desireds = desireds + i + " " 
     #print desireds 
     f.write(desireds) 
f.close() 

あなたが1列に出力を取得したい場合。上記のコードで

、ちょうどdesireds = desireds + i + " "

f = open("ms.ctl", "rb") 
    s = f.read() 
    f.close() 

    N = s.split('\n') 
    lenth = len(N) 

    f = open("newms.ctl", "wb") 

    for x in range(0, lenth): 
      print "We're on time %d" % (x) 
      spliteds = [ x for x in N[0].split('/') if x.strip()] 
      desireds="" 
      for i in reversed(spliteds): 
        desireds = desireds + i + "\n" 
      #print desireds 
      f.write(desireds) 
    f.close() 
+0

あなたの答えをありがとうが、親切に全機能を書いてください。何らかの形でms.ctlの現在の形式はs = mycnf_001/mycnf_001_001で、目的の形式はmycnf_001_001 mycnf_001 – Andy

+0

(目的の形式はmycnf_001_001onespacemycnf_001で構成されています)実際には発言IDの後に発言者ID – Andy

+0

が追加されています。それが役に立てば幸い。何か問題がある場合はお知らせください。 – Rocoder

関連する問題