2017-05-22 82 views
0

我想將輸出的打印語句重定向到文本文件。我導入了系統並在下面嘗試。將stdout重定向到Python中的文本文件?

import pprint 
import os 
import math 
import sys 

class ExportLimits(object): 
    MMAP_ITER_TRIPS = 'manpower_mappings.map_iterator_trips' 

def __init__(self, workset, crew_type, bid_period, divisor_files=None): 
    log_file = "/opt/test/test_user/data/ESR_DATA/etab/slogfile.txt" 
    sys.stdout = open(log_file, 'w') 
    self.workset = workset 
    self.crew_type = crew_type 
    self.bid_period = bid_period 
    self.tm = workset.getTM() 
    self.crew_bid_period = self.crew_type + "+" + self.bid_period 
    self.bid_period = self.tm.table('qf_user_bid_period')[(self.crew_bid_period)] 
    self.period = Period(self.bid_period.bpstart, self.bid_period.bpend) 
    self.filter_matcher = self._get_filter_matcher() 
    self.iterator_trips = rave_api.eval(\ 
      ExportLimits.MMAP_ITER_TRIPS)[0] 
    self.divisor_reader_lh = divisor_reader_lh.DivisorReader(\ 
      divisor_files=divisor_files) 
    self.divisor_reader_sh = divisor_reader_sh.DivisorReader(\ 
      divisor_files=divisor_files) 
    self.pp_start = self.period.getStart() 
    self.pp_end = self.period.getEnd() 

def export_limits(self, item_type): 
    if item_type == 'DKSH': 
     self._mandays_limits(SLKHH_GROUPS) 
    else: 
     self._mandays_limits(LAJSDLH_GROUPS) 
def _mandays_limits(self, groups): 
    crews = [self.tm.table('crew')[('99172447',)], 
      self.tm.table('crew')[('7654678',)]] 
    generator = ((crew, self.filter_matcher.getFilterNamePeriodsMap(crew.id)) 
       for crew in self.tm.table('crew')) 

    minres = defaultdict(lambda :RelTime(0)) 
    maxres = defaultdict(lambda :RelTime(0)) 

    for crew, group_to_periods in generator: 
     print crew, group_to_periods 
     try: 
     crew_filter, period = group_to_periods.iteritems().next() 
     except StopIteration: 
     continue 
     if crew_filter not in groups: 
     continue 

它的工作原理部分對我來說。我能夠打印幾行,但不完全。請在我的日誌文件的下面的輸出中找到它只打印較少的行但不打印完整的日誌。 出於某種原因,它沒有完全打印。 (請看看它印只能做到 「特別」 的日誌文件的最後一行。)

日誌文件:

船員_id = 「133245」 ID = 「176543」 EMPNO = 「8761890」 性=「M」 birthday =「19681217」name =「MICHEAL」forenames =「LUCAS」maincat =「C」 preferredname =「ESWAR」initials =「LL」joindate =「20010910 00:00」 comjoindate =「20010910 00 :00" _void = 「標題,日誌名,SI,bcity,bstate,bcountry,別名comenddate」{ 'X-SYD-BB-AUSLLH':[26JUN2017 00:00-21AUG2017 00:00]}

crew _id =「214141」id =「132451」em pno =「145432」sex =「M」 birthday =「19630904」name =「ESWARF」forenames =「FJDJSK」maincat =「C」 preferredname =「ESWADF」initials =「WL」joindate =「20010910 00:00」 comjoindate = 「20010910 00:00」 _void =「稱號,LOGNAME,SI,bcity,bstate,bcountry,特別


請檢查和諮詢。

+1

查看python日誌記錄模塊! https://docs.python.org/3/library/logging.html – caffreyd

+0

你有沒有嘗試在'open'中觸摸緩衝參數? https://docs.python.org/3/library/functions.html#open – grovina

+0

@grovina:我還沒有嘗試緩衝參數。我可以嘗試一下,讓你知道一段時間。 – rawse

回答

0

而不是使用sys.stdout的,你可以這樣寫的:

output_file = open(log_file, 'w') 

output_file.write('testing asdasdfsd') 

或者,如果你想要寫在日誌文件中的所有種類的打印值,則:

OUTPUT_FILE =開放(LOG_FILE,「W ')

sys.stdout = output_file

就是這樣。

相關問題