tickettypepat = (r'MIS Notes:.*(//p//)?.*')
retype = re.search(tickettypepat,line)
if retype:
print retype.group(0)
print retype.group(1)
入力が与えられます。
MIS Notes: //p//
group(0) がなぜなのか誰か教えてもらえますか
MIS Notes: //p//
group(1) は None として返されますか?
問題が発生する前に、マッチングは単なるマッチングよりも複雑だったので、私はもともと正規表現を使用していました //p// ここに完全なコードがあります。私はこれにかなり慣れていないので、私の初心者を許してください。これの多くを達成するためのより良い方法があると確信しています。しかし、//[pewPEW]// の正規表現が貪欲すぎるという問題を除けば、機能しているようです。助けてくれてありがとう。
テキストを取り、いくつかのものをクリーンアップ/変換します。
filename = (r'.\4-12_4-26.txt')
import re
import sys
#Clean up output from the web to ensure that you have one catagory per line
f = open(filename)
w = open('cleantext.txt','w')
origdatepat = (r'(Ticket Date: )([0-9]+/[0-9]+/[0-9]+),( [0-9]+:[0-9]+ [PA]M)')
tickettypepat = (r'MIS Notes:.*(//[pewPEW]//)?.*')
print 'Begining Blank Line Removal'
for line in f:
redate = re.search(origdatepat,line)
retype = re.search(tickettypepat,line)
if line == ' \n':
line = ''
print 'Removing blank Line'
#remove ',' from time and date line
elif redate:
line = redate.group(1) + redate.group(2)+ redate.group(3)+'\n'
print 'Redating... ' + line
elif retype:
print retype.group(0)
print retype.group(1)
if retype.group(1) == '//p//':
line = line + 'Type: Phone\n'
print 'Setting type for... ' + line
elif retype.group(1) == '//e//':
line = line + 'Type: Email\n'
print 'Setting type for... ' + line
elif retype.group(1) == '//w//':
line = line + 'Type: Walk-in\n'
print 'Setting type for... ' + line
elif retype.group(1) == ('' or None):
line = line + 'Type: Ticket\n'
print 'Setting type for... ' + line
w.write(line)
print 'Closing Files'
f.close()
w.close()
そして、ここにいくつかのサンプル入力があります。
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes: some random stuff //p// followed by more stuff
Key Words:
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes: //p//
Key Words:
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes: //e// stuff....
Key Words:
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes:
Key Words: