import re
import os
import sys
data_record=re.compile('(.{4}) (.{7}) (.{3}-.{4}-.{1}|.{10}) (\d{2}/\d{2}/\d{2}) (\d{2}:\d{2}:\d{2}) (\d{1,3}[a-z]{1,8})([ ]{8}.+)')
KYC= open(r"C:\path\Untitled.txt")
#head=list(islice(KYC, 10))
#print head
KYCapr2017=sys.stdout=open("KYC-apr2017.txt","w")
for line in KYC:
matchData=data_record.match(line)
if matchData:
print (matchData.group(1).strip() + ","
+ matchData.group(2).strip() + ","
+ matchData.group(3).strip() + ","
+ matchData.group(4).strip() + ","
+ matchData.group(5).strip() + ","
+ matchData.group(6).strip() + ","
+ matchData.group(7).strip()
)
sys.stdout.close()
在使用模块'sys'添加行之前,我能够使用F5在Spyder中一致地运行上面的代码。由于添加了这些行(将打印输出保存到txt文件),它只运行一次。要再次运行它,我必须重新启动spyder。怎么样?