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: