Chat.txt텍스트/워드는 위에서 언급 한 텍스트에 파이썬에서 파일에
ID674 25/01/1986 Thank you for choosing Optimus prime. Please wait for an Optimus prime Representative to respond. You are currently number 0 in the queue. You should be connected to an agent in approximately 0 minutes.. You are now chatting with 'Tom' 0 <br/>
ID674 2gb Hi there! Welcome to Optus Web Chat 0/0/0 . How can I help you today? 1
ID674 25-01-1986 I would like to change my bill plan from $0 with 0 expiry to something else $136. I find it very unuseful. Sam my phone no is 9838383821 2
를 계산하는 것은 file.My 요구에 몇 줄의 예에 불과은 그 예를 25/01에 대한 모든 날짜/1986 또는 0/0/0은 "DATE123"으로 대체해야합니다.
다음 :)은 "smileys123"으로 바꿔야합니다. 통화 즉, $ 0 또는 $ 136은 "Currency123"으로 대체되어야합니다.
'TOM'(일반적으로 작은 따옴표로 묶인 상담원 이름)은 AGENT123
이상으로 바꿔야합니다. 출력은 다음과 같이 문자열 발생 횟수로 표시되어야합니다. $ 0 또는 $ 136 "Currency123 '와'TOM '(일반적으로 대체해야
class Replace:
dateformat=DATE123
smileys=smileys123
currency=currency123
count_dict={}
function count_data(type,count):
global count_dict
if type in count_dict:
count_dict[type]+=count
else:
count_dict = {type:count}
f=open("chat.txt")
while True:
for line in f.readlines():
print line,
if ":)" in line:
smileys = line.count(":)")
count_data("smileys",smileys)
elif "$number" in line : #how to see whether it is currency or nor??
currency=line.count("$number") //how can i do this
count_data("currecny",currency)
elif "1/2/3" in line : #how to validate date format
dateformat=line.count("dateformat") #how can i do this
count_data("currency",currency)
elif validate-agent-name in line:
agent_name=line.count("agentname") #How to do this get agentname in single quotes
count_data("agent_name",agent_name)
else:
break
f.close()
for keys in count_dict:
print keys,count_dict[keys]
The following would be the ouput
DATE123=2 smileys123=2 Currency123=6 AGENT123=5
PEP8 (http://www.python.org/dev/peps/pep-0008/), 아니요 의도 된 범죄. – Benjamin
당신은 정규 표현식에 대해 알고 있습니다. – Marcin
필요한 경우 각 패턴의 발생 횟수를 계산하는 것만으로 파일의 텍스트를 바꿀 필요가 없습니다. 그냥're.findall()'을 사용하십시오. –