Please help with script...

Topy hobiewon at hotmail.com
Tue Jun 4 00:14:02 EDT 2002


Hey all,
I'm having trouble with a problem I have with this python program.  It's
kind of long, so if anyone has the patience, I would gladly appreciate your
time(the programming is fairly simple I think.)  Anyway, the problem I need
to do is this :

I am given a floppy disk containing CSV (comma separated values).  Each
record (row) of the data contains 5 data-elements:
o OrderID (a five digit number) indicating the order number,
o OrderDate - a date-field indicating the date the order was placed
o RegionID - a single digit indicating the region where the order was
manufactured
o ProductID - a two-digit code indicating the product number
o OrderQty - an integer indicating the order size.

    I am interested in analyzing this sales data based on the product type
and the order quantity and would like to have this information extracted
from the CSV file into a spreadsheet. For example, only orders for ProductID
9 having an OrderQty greater than 1600 are to be included in the worksheet.

Write a Python program that will first ask the user:
1) Which ProductID is of interest
2) What is the minimum OrderQty of interest.

The program will then read the file (it will always be "a:\sales.txt") and
create a new workbook in excel named "ProductSales.xls" (in the directory
where the CSV file is) with a sheet named after the ProductID of interest,
e.g., "9".  The first row of the sheet should list the minimum amount of the
sales, e.g., "Minimum OrderQty: 1600" in Cell(A1). Each row thereafter
should contain the OrderID (in Column1), OrderDate (in Column 2) and the
OrderQty (in Column 3) for each record in the sales file that meets the
requirements.

The program should work on a file based on any number of sales transactions;
test it on the file included.  Below, are the first seven lines of the CSV
file:

19034,4/1/02,1,7,1732
19035,4/1/02,1,9,1888
19036,4/1/02,1,4,1048
19037,4/1/02,1,5,1708
19038,4/1/02,1,6,876
19048,4/2/02,1,10,472
19049,4/2/02,1,9,1596
..

If the Sales Manager is interested in orders for ProductID 9 that are
greater than 1600, then based on the data in the first seven lines, your
Python program should select line 2 and enter the required elements from
this line in the spreadsheet.

Here is an approximate outline of what my program is supposed to look like:
1. Use the function raw_input() to get the ProductID and the cut-off order
quantity from the user.
2. Note that the raw_input() function will return the order quantity as a
string while you want to use order quantity as a float when you make
comparisons. So you will need an additional line of code that uses the
float() function to convert the "string" OrderQty into a "float" OrderQty
3. Open the CSV file using the open() function
4. Import the Excel client, open an instance of the Excel application and
add a workbook to it. (Refer to class notes or IDT book for this).
5. You could use the readlines() function along with a for loop OR the
readline() function with a while loop to read through the lines in the CSV
file that you opened in Step4.
6. For each line read from the CSV file:
a. Use the split() function (with ',' comma-delimiter) to break the line
into its components (OrderID, OrderDate, etc).
b. Note that the last item returned by the split() function, which is
OrderQty, will have a newline character appended to it. You will have to
strip off the newline character and convert the OrderQty into a float (like
we did in step 2), before you will be able to use it.
c. If the ProductID and OrderQty for the current record satisfy the criteria
specified by the user, then insert this information into the Excel
spreadsheet


Anyway, so far this is what I have :

ProductID = float(raw_input("Please enter the ProductID: "))
OrderQty = float(raw_input("Please enter the cutoff order quantity: "))
CSV = open('a:/sales.txt', 'r')
import win32com.client
x1 = win32com.client.Dispatch('Excel.Application')
x1.Visible=1
x1.Workbooks.Add()


It's not much, but I'm not sure where to go from here...can anyone help?

Thanks so much!
Topy







More information about the Python-list mailing list