2016-06-30 10 views
-5

は簡単なプログラムを書いており、出力をテキストファイルに出力したいと考えています。私はスクリプトの一番上で次のことを試しています:pythonでtxtファイルに出力する

python SagicorPremiumCalculator2.py > textfile.txt 

これは私にアイドル時の構文エラーです。構文はどうやって間違っていますか?

python SagicorPremiumCalculator2.py > textfile.txt 

python SagicorPremiumCalculator2.py > textfile.txt 
import openpyxl, os, sys 
wb = openpyxl.load_workbook('example.xlsx') 
Millar_sheet = wb.get_sheet_by_name('Millar') 
client = [] 
output = [] 
print(os.getcwd()) 


for rowOfCellObjects in Millar_sheet['A2':'AA13']: 
     for cellObj in rowOfCellObjects: 
       #if cellObj.value != None: 
       #print(cellObj.coordinate, cellObj.value) 
       client.append(cellObj.value) 
     #print(client[2]) #client name 
     print(client[0]) #policy number 
     print(client[9]) #license plate 
     #output.append(client[0]) #1 
     #output.append(client[9]) #2 
     if client[12] != "Not Applicable": 
      float(client[12]) 
      print('S.I. = ' + '$' + str("%0.2f" % client[12])) 
      #output.append("%0.2f" % client[12]) #3 
     else: 
      print('S.I. ' + client[12]) 
      print('Basic = ' + '$' + str("%0.2f" % client[13])) 
      #output.append("%0.2f" % client[13])#3 
     if client[14] != None: 
      print('Loading = ' + str(100*client[14]) + '%') 
     else: 
      print('No Loading') 
     val = client[13] 
     if client[14] == None: 
      val = client[15]  #if there is no loading percentage, val becomes subtotal 
     else: 
      val += (val*client[14]) # a = a + b, adds 150 to 1000 
     print('Subtotal = ' + '$' + str("%0.2f" % val))#good 
     if client[16] != None: 
      ATD = val*client[16] #discount amount 
      print('ATD = ' + str(100*client[16]) + '%, -$' + str("%0.2f" % ATD)) 
      val -= (val*client[16]) 
      print('$' + str("%0.2f" % val)) 
     if client[17] != None: 
      val -= (val*client[17]) 
      SP = val*client[17] #Discount amount 
      print('SP = ' + str(100*client[17]) + '%, -$' + str("%0.2f" % SP)) 
      print('$' + str("%0.2f" % val)) 
     if client[18] != None: 
       val = (val+client[18]) 
       PAB = client[18] 
       print('PAB = +$' + str(client[18])) 
       print('$' + str("%0.2f" % val)) 
     if client[19] != None: 
      NCD = val*client[19] 
      val -= (val*client[19])#discount amount 
      print('NCD = ' + str(100*client[19]) +'%, -$' + str("%0.2f" % NCD)) 
      print('$' + "%0.2f" % val) 
     if client[20] != None: 
      val = (val+client[20]) 
      print('W/S = +$' + str(client[20])) 
      print('$' + "%0.2f" % val) 
     if client[21] != None: 
      val = (val+client[21]) 
      print('LOU = $' + str(client[21])) 
      print('$' + "%0.2f" % val) 
     if val < 750: #Applies minimum premium if val is under 750 
      print("Minimum premium applied") 
      val = 750 

     print('Pre-tax Premium is ' + '$' + str("%0.2f" % val)) #good 
     if client[23] == 0: #Client over 65, not being charged tax 
       print('According to Sagicor speadsheet, client over 65') 
     else: 
       PreTax = val*0.06 
       val = (PreTax+val) 
       print('Premium Tax = +$' + str("%0.2f" % PreTax)) #good 
       print('$' + "%0.2f" % val) 
     if client[25] != None: 
       print('RS = +$' + str(client[25])) 
       val = (val+client[25]) 
       print('Total = $' + str("%0.2f" % val)) 
       #if val == client[26]: 
         #print('Sagicor\'s total agrees with calculated total - top') 
       #else: 
         #print('Premium does not agree - top') 

     else: 
       print('Roadside assistance NOT included') 
       print('Total = $' + str("%0.2f" % val)) 
       #if val == client[26]: 
         #print('Sagicor\'s total agrees with calculated total - bottom') 
       #else: 
         #print('Premium does not agree - bottom') 
     client = [] 
     output = [] 
     print('--- END OF ROW ---') 
+0

上記のシンタックスは、シェルスクリプトであり、Pythonスクリプトではありません。 – nbryans

+1

あなたは* Pythonスクリプトの中でそれをしようとしていますか?*これは有効なPythonではない、それはシェルコマンドです。 – jonrsharpe

+1

SagicorPreniumCalculator2.pyのコードを教えてください。出力をファイルにリダイレクトせずに使用しているときに、何を印刷しますか? –

答えて

0

スクリプトの出力をファイルに書きたい場合は、2つのオプションがあります。 最初のものは、Pythonスクリプトで書き込みを処理することです:

#!/usr/bin/python 
#I am here assuming that you use a Unix-like system (i.e. Not Windows) 

file = open('textfile.txt','w') 

... some code ... 

file.write(myData +'\n') 

#Don't forget to close the file at the end of the script 
file.close() 

N.B:それは存在していない場合は、「W」オプションは、ファイルが作成されますが、それは、ファイルの以前のバージョンを削除aslo。既存のファイルの最後に行を追加する場合は、代わりに 'a'を使用してください。 '\ n'は新しい行を作成するためのものです。

他のオプションは以下の通りである。その後、簡単なスクリプトで使用する印刷statament

#My Script 
... some code ... 
print MyData 

と、次のコマンドでシェルIDLEでないでスクリプトを実行します。

python SagicorPremiumCalculator2.py > textfile.txt 

Windowsを実行している場合は、最初のオプションを使用する必要があります(bash形式のコマンドがcmdで機能するかどうかわからないため)。

こちらがお役に立てば幸いです。

+0

私に説明する時間をとっていただきありがとうございます。私は窓で作業しています。私はちょうどそのフォーマットされた方法で遊ぶ必要があります。 – Jim

関連する問題