2017-09-25 10 views
1

リストと文字列の書式設定に問題があり、変更を新しいファイルに書き込んでいます。私が探しているです: リストと文字列のフォーマットの問題:txtファイルの読み込み、文字列の追加と新しいファイルへの書き込み

  • インポートtxtファイルの内容(文字列値のリスト)の両方の前後の文字列がすでにある
  • AFTER

  • STRINGS、BEFORE

    1. STRINGS定義され、すべてが新しいファイルに書き込まれます!

      txtファイル(リストを含む)をインポートしてコードを実行すると、インポートされたtxtファイルのリストの前後にあらかじめ定義された文字列が追加された新しいファイルに印刷されます。次のように今

      私のコードは次のとおりです。

      text_file = open(r"text_file path", "r") 
      lines = text_file.read().split(',') 
      lines.insert(0, "String Values Before") 
      lines.insert("String Values After") 
      text_file.close() 
      lines.write("new_file.txt", "w+") 
      

      私は文字列がリストに別々にしたいのに対し、問題は今、私はリストに入れていますということです!

      私は私が書かれたファイルはここにこのコードを使用してコンソールに見えるようにしたいものを生成することができました:私はちょうど新しいファイルにこれを書き込む方法を見つけ出すことはできません

      FIRMNAME = "apple" 
      FILETYPE = "apple" 
      REPLYFILENAME = "apple" 
      SECMASTER = "apple" 
      PROGRAMNAME = "apple" 
      
      text_file = open(r"textfile path", "r+") 
      lines = text_file.readlines().split('\n') 
      
      print(("START-OF-FILE \nFIRMNAME= ") + FIRMNAME) 
      
      print(("FILETYPE= ") + FILETYPE) 
      
      print(("REPLYFILENAME= ") + REPLYFILENAME) 
      
      print(("SECMASTER= ") + SECMASTER) 
      
      print(("PROGRAMNAME= ") + PROGRAMNAME) 
      
      
      print("START-OF-FIELDS") 
      
      print("END-OF-FIELDS") 
      
      print("START-OF-DATA") 
      pprint.pprint(lines) 
      print("END-OF-DATA") 
      print("END-OF-FILE") 
      

      !助けて!

  • +0

    あなたはこれで何を意味するの追加モードと改行 –

    +0

    を使用しますか?あなたは私を見せてくれますか?ありがとう! : –

    +1

    'lines.write(" new_file.txt "、" a ")' –

    答えて

    2

    あなたはこの方法でそれを解決することができます:

    newFile = 'your_new_file.txt' 
    oldFile = 'your_old_file.txt' 
    
    # Open the new text file 
    with open(newFile, 'w') as new_file: 
        # Open the old text file 
        with open(oldFile, 'r') as old_file: 
         # Write the line before the old content 
         new_file.write('Line before old content\n') 
    
         # Write old content 
         for line in old_file.readlines(): 
          new_file.write(line) 
    
         # Write line after old content 
         new_file.write('Line after old content') 
    
    +1

    ミッチーありがとう!出来た! :) –

    1

    はあなたの変数linesはメソッドwriteを持たない、タイプlistです。
    さらにinsertには、2番目の呼び出しに欠けている位置が必要です。

    あなたは、ファイルを読むために必要に応じて接頭辞と接尾辞の値でそれをCONCATし、適切な出力ファイルに記述します:

    with open("text_file_path", "r") as input_file: 
        text = input_file.read() 
    
    text = '\n'.join(("String Values Before", text, "String Values After")) 
    
    with open("new_file.txt", "w+") as output_file: 
        output_file.write(text) 
    
    1

    使用pformat
    pprint

    before_values = ["a", "b", "c"] 
    data = ["1", "2", "3"] 
    after_values = ["d", "e", "f"] 
    with open("outfile.txt", "w) as outfile: 
        outfile.write("\n".join(before_values)) # Write before values 
        outfile.write(pprint.pformat(data))  # Write data list 
        outfile.write("\n".join(after_values)) # Write after values 
    
    0

    あなたは、インデックスを提供しなければならないとして、あなたはもともとinsertメソッドを呼び出して、エラーを持っています。しかし、あなただけの、追加結果のリストに参加し、ファイルに書き込むことができます。

    text_file = open(r"text_file path", "r") 
    lines = text_file.read().split(',') 
    lines.insert(0, "String Values Before") 
    lines.append("String Values After") 
    text_file.close() 
    new_file = open('text_file_path.txt', 'w') 
    new_file.write(','.join(lines)+'\n') 
    new_file.close() 
    
    関連する問題