2016-06-30 2 views
-5

간단한 프로그램을 작성하여 출력을 텍스트 파일로 인쇄하려고합니다. 내 스크립트의 맨 위에 다음을 시도하고 있습니다.파이썬에서 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

위 구문은 파이썬 스크립트가 아닌 쉘 스크립트입니다. – nbryans

+1

파이썬 스크립트 안에서 * 수행하려고합니까? * 유효한 파이썬이 아니며 쉘 명령입니다. – jonrsharpe

+1

당신은 우리에게 SagicorPreniumCalculator2.py의 코드를 제공 할 수 있습니까? 파일의 출력을 리다이렉션하지 않고 출력 할 때 출력되는 것은 무엇입니까? –

답변

0

스크립트 출력을 파일에 기록하려면 두 가지 옵션이 있습니다. 그렇지 않은 존재하는 경우 'w'옵션은 파일을 생성하지만 파일의 이전 버전을 삭제 끄트머리 :

#!/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 : 첫 번째는 파이썬 스크립트에서 쓰기를 처리하는 것입니다. 기존 파일의 끝에 줄을 추가하려면 대신 'a'를 사용해야합니다. '\ n'은 새 행을 만드는 데 사용됩니다.

python SagicorPremiumCalculator2.py > textfile.txt 
: 다음 명령을 IDLE하지 쉘 에서 스크립트를 실행

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

다음 스크립트의 간단한 사용 인쇄 statament :

다른 옵션은 다음과 같다

Windows를 사용하는 경우 첫 번째 옵션을 사용해야합니다 (왜냐하면 bash 스타일 명령이 cmd에서 작동하는지 잘 모르기 때문입니다).

도움이 되었기를 바랍니다.

+0

시간을내어 설명해 주셔서 감사합니다. 나는 창문에서 일하고있다. 이제 형식이 어떻게 바뀌 었는지 알아볼 필요가 있습니다. – Jim

관련 문제