Advertisement

09.11.2008 at 07:18PM PDT, ID: 23725181
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.1

Python script won't filter

Asked by rtsy in Python Scripting Language

Tags: , ,

Hi All,

What I am trying to do is to combine two existing python scripts/reports into one.  The first is set up to run WEEKLY, and filters correctly for a value called campaign_no.  The second is my new script, set up to run DAILY, and also filters for the campaign_no.  I did get this new, combined report to run daily (no errors). But, for some reason, it returns everything in the campaign field and does not filter to the specific one I need.  

I am sorry that I am new to Python and do not have a good handle yet on what might be wrong.  I have been beating my head on this for the last several hours, even trying to build a new one off of other working reports. Still, I get the same results - a return of all records and not a filtered report.

I'll post the two scripts below.  I'm new here, and I have been advised to only post a snippet of the script, but I'm not sure which part is wrong, so I'm not sure which parts to post.  Please forgive me for posting too much.  The first is the one which runs weekly, (where I got the filtered section).  The second is my combined script which does run daily, but does not correctly filter on the campaign_no, (it returns all the records in the source report, not just the filtered ones).  

Would any of you fine folks have any suggestions as to either why the script won't filter, or perhaps could help me with the reprogramming of the weekly script to change it to run daily?  ANY help you could throw my way would be so appreciated.

Thank you all for your time.
rtsyStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
-----FIRST SCRIPT-----
## E_D-Weekly report
## runs on Fri after raw reports complete
## uses CDR Plus w/Notes
 
from datetime import *
from os.path import *
from os import *
from csv import *
import csv
 
rpt_name = 'E_D-Weekly'
fpath = 'D:/Users/Reports/' # production path
#fpath = 'Z:/Reports/'  #test path
suffix = '.csv' # file suffix
dateformat = '%Y-%m-%d'  # dateformat prefix for filenames
logpath = 'D:/Users/Reports/report-log.txt' # production path
#logpath = 'Z:/Reports/report-log.txt'  #test path
timestamp = datetime.now() # snap the datetime
 
# report run constants (verified correct)
iso_run_day = 5  #Friday
 
# create a log entry
if exists(logpath):
        ff = file(logpath, 'ab')
else:
        ff = file(logpath, 'w+b')
	
	
ff.write(str(timestamp) +',E_D-Weekly Report Launched\n')
 
if not (timestamp.isoweekday() == iso_run_day):  # see if we should generate the report
		timestamp = datetime.now()
		ff.write(str(timestamp) + ',E_D-Weekly Report:no report generated.\n')
		ff.close()
else:
        # generate the report
        ord_offset = -8 #  previous Thursday
        ord_end_day = 6 # Wednesday night
        source_rpt = 'CDR-w-Notes' #folder and base report name
        date_col = 'start_date'
        filt_col = 'campaign_no'
        filt_value = '6610' #E_D campaign
        source_cols = [
                'start_date', 
                'start_time',
                'skill_name',
                'contact_name',
                'ANI_DIALNUM',
                'agent_name',
                'PreQueue',
                'InQueue',
                'Routing_Time',
                'PostQueue',
                'Agent_Time',
                'ACW_Time',
                'Total_Time',
                'callback_time',
                'Hold_Time',
                'Disp_Code',
                'Disp_Name',
                'Disp_Comments']
        #excluded dispostions: Test Call, TestCall(2)
        disp_exclude =['8001669', '8001683']
        begin_date = datetime.fromordinal(timestamp.toordinal() + ord_offset)
        end_date = datetime.fromordinal(begin_date.toordinal() + ord_end_day)
        #print begin_date.date(), '--', end_date.date()
        file_list = []
        # make source file list
        for d in range((end_date.toordinal() - begin_date.toordinal()) + 1):
                file_list += [ fpath + source_rpt + '/' + str(date.fromordinal(begin_date.toordinal() + d)) + source_rpt + suffix ]
        #print file_list
        rows = []
        for blk in file_list:
            #print blk
            if exists(blk):  # don't worry about missing files....
                    f = file(blk, "rb")
                    tmp = DictReader(f)
                    for k in tmp:
                            if (k[filt_col] == filt_value) and (k['Disp_Code'] not in disp_exclude):
                                    jtmp = {}
                                    for j in source_cols:
                                            jtmp.update({j:k[j]})
                                    rows += [jtmp] 
                    f.close()
        
        o_rows = [['Start Time',
                        'DNIS', 
                        'ANI',
                        'Pre-Queue Time',
                        'Queue Time',
                        'Routing Time',
                        'Talk Time',
                        'ACW Time',
                        'Transfer Time',
                        'Non-Interactive',
                        'Interactive',
                        'Total Duration', 
                        'Agent Name',
                        'Dispostion',
                        'Comments']]
 
        for row in rows:
                if (row['Disp_Code'] == '8001670') and (row['contact_name'] == row['ANI_DIALNUM']):
                        non_inter = float(row['Total_Time'])/60.0
                        inter = 0.0
                        transfer = non_inter
                        inter += float(row['ACW_Time'])/60.0
                        inter += float(row['Hold_Time'])/60.0
                else:
                        non_inter = float(row['PreQueue'])/60.0
                        non_inter += float(row['InQueue'])/60.0
                        non_inter += float(row['Routing_Time'])/60.0
                        non_inter += float(row['PostQueue'])/60.0
                        non_inter += float(row['callback_time'])/60.0  # zero for now, but if we start using this feature...
                        inter = float(row['Agent_Time'])/60.0
                        inter += float(row['ACW_Time'])/60.0
                        inter += float(row['Hold_Time'])/60.0
                        transfer = float(row['PostQueue'])/60.0
                o_rows += [[
                        row['start_date'] + ' ' + row['start_time'],
                        row['contact_name'],
                        row['ANI_DIALNUM'],
                        float(row['PreQueue'])/60.0,
                        float(row['InQueue'])/60.0,
                        float(row['Routing_Time'])/60.0,
                        float(row['Agent_Time'])/60.0,
                        float(row['ACW_Time'])/60.0,
                        transfer,
                        non_inter,
                        inter,
                        float(row['Total_Time'])/60.0,
                        row['agent_name'],
                        row['Disp_Name'],
                        '"' + str(row['Disp_Comments']) + '"']]
        if not exists(fpath + rpt_name):
            mkdir(fpath + rpt_name)
        fname = fpath + rpt_name + '/' + begin_date.strftime('%m-%d--') + end_date.strftime('%m-%d-%y')+rpt_name + suffix
        
        f = file(fname, 'wb')
 
        writer = csv.writer(f)
        writer.writerows(o_rows)
        f.close()
        timestamp = datetime.now()
        ff.write(str(timestamp) + ',E_D-Weekly Report Generated.\n')
        ff.close()
 
 
 
----SECOND SCRIPT-----
## P_G-Daily.py
## runs everyday after raw reports complete
## uses CDR Plus w/Notes
 
from datetime import *
from os.path import *
from os import *
from csv import *
import csv
 
rpt_name = 'P_G-Daily'
fpath = 'D:/Users/Reports/' # production path
#fpath = 'Z:/Reports/'  #test path
suffix = '.csv' # file suffix
dateformat = '%Y-%m-%d'  # dateformat prefix for filenames
logpath = 'D:/Users/Reports/report-log.txt' # production path
#logpath = 'Z:/Reports/report-log.txt'  #test path
timestamp = datetime.now() # snap the datetime
 
# report run constants (verified correct)
#  Runs every day
 
# create a log entry
if exists(logpath):
        ff = file(logpath, 'ab')
else:
        ff = file(logpath, 'w+b')	
 
ff.write(str(timestamp) +',P_G-Daily\n')
 
# generate the report
ord_offset = -1 #  previous day
ord_end_day = 0
source_rpt = 'CDR-w-Notes' #folder and base report name
date_col = 'start_date'
filt_col = 'campaign_no'
filt_value = '7060' #P_G campaign
source_cols = [
        'start_date', 
        'start_time',
        'skill_name',
        'contact_name',
        'ANI_DIALNUM',
        'agent_name',
        'PreQueue',
        'InQueue',
        'Agent_Time',
        'PostQueue',
        'ACW_Time',
        'Total_Time',
        'Abandon_Time',
        'Routing_Time',
        'abandon',
        'callback_time',
        'Logged',
        'Hold_Time',
        'Disp_Name',
        'Disp_Comments']
#excluded dispostions: Test Call, TestCall(2)
disp_exclude =['8001669', '8001683']
begin_date = datetime.fromordinal(timestamp.toordinal() + ord_offset)
end_date = datetime.fromordinal(begin_date.toordinal() + ord_end_day)
#print begin_date.date(), '--', end_date.date()
file_list = []
# make source file list
for d in range((end_date.toordinal() - begin_date.toordinal()) + 1):
        file_list += [ fpath + source_rpt + '/' + str(date.fromordinal(begin_date.toordinal() + d)) + source_rpt + suffix ]
#print file_list
rows = []
for blk in file_list:
    #print blk
    if exists(blk):  # don't worry about missing files....
            f = file(blk, "rb")
            tmp = DictReader(f)
            for k in tmp:
                    jtmp = {}
                    for j in source_cols:
                            jtmp.update({j:k[j]})
                    rows += [jtmp] 
            f.close()
 
o_rows = [['Start Time',
        'skill_name',
        'contact_name',
        'ANI_DIALNUM',
        'agent_name',
        'PreQueue',
        'InQueue',
        'Agent_Time',
        'PostQueue',
        'ACW_Time',
        'Total_Time',
        'Abandon_Time',
        'Routing_Time',
        'abandon',
        'callback_time',
        'Logged',
        'Hold_Time',
        'Disp_Name',
        'Disp_Comments']]
 
for row in rows:
        o_rows += [[
                row['start_date'] + ' ' + row['start_time'],
                row['skill_name'],
                row['contact_name'],
                row['ANI_DIALNUM'],
                row['agent_name'],
                row['PreQueue'],
                row['InQueue'],
                row['Agent_Time'],
                row['PostQueue'],
                row['ACW_Time'],
                row['Total_Time'],
                row['Abandon_Time'],
                row['Routing_Time'],
                row['abandon'],
                row['callback_time'],
                row['Logged'],
                row['Hold_Time'],
                row['Disp_Name'],
                '"' + str(row['Disp_Comments']) + '"']]
if not exists(fpath + rpt_name):
    mkdir(fpath + rpt_name)
fname = fpath + rpt_name + '/' + begin_date.strftime('%m-%d--') + end_date.strftime('%m-%d-%y')+rpt_name + suffix
 
f = file(fname, 'wb')
 
writer = csv.writer(f)
writer.writerows(o_rows)
f.close()
timestamp = datetime.now()
ff.write(str(timestamp) + ',P_G-Daily Report Generated.\n')
ff.close()
 
Loading Advertisement...
 
[+][-]09.11.2008 at 10:07PM PDT, ID: 22457275

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Python Scripting Language
Tags: Python.org, Python, 2.5.1
Sign Up Now!
Solution Provided By: clockwatcher
Participating Experts: 3
Solution Grade: A
 
 
[+][-]09.11.2008 at 11:26PM PDT, ID: 22457499

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]09.13.2008 at 05:02AM PDT, ID: 22466759

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628