[Tutor] CVS File Opening

Paras K. paras80 at gmail.com
Tue May 26 20:24:35 CEST 2009


I have been working on this script / program all weekend. I emailed this
address before and got some great help. I hope that I can get that again!

First to explain what I need to do:

Have about 6 CSV files that I need to read. Then I need to split based on a
range of IP address and if the count number is larger than 75.

I currently merge all the CSV files by using the command line:

C:Reports> copy *.csv merge.csv

Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in ('find "."
merge.csv ^| find /v "----"') do echo %i>> P2PMerge.csv

>From some of my friends they tell me that should remove that last carriage
return, which it does, however when it goes through the python script it
returns no values.

Now if I open the merge.csv and remove that carriage return manually and
save it as P2PMerge.csv the script runs just fine.

Here is my source code:

# P2P Report / Bitorrent Report
# Version 1.0
# Last Updated: May 26, 2009
# This script is designed to go through the cvs files and find the valid IP
# Then copys them all to a new file
import sys
import win32api
import win32ui
import shutil
import string
import os
import os.path
import csv
#Global Variables
P2Pfiles = []
totalcount = 0
t = 0

#still in the development process -- where to get the files from
#right now the location is C:\P2P
def getp2preportdestion():
    win32ui.MessageBox('Welcome to P2P Reporting.\nThis program is designed
to aid in the P2P reporting. \n\nThe locations of P2P Reports should be in
C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" to continue
with this program.')
    p2preport = 'C://P2P\\'
    return p2preport

#Main Program
#Get location of directories
p2ploc = getp2preportdestion()
#Checking to make sure directory is there.
if os.path.exists(p2ploc):
    if os.path.isfile(p2ploc +'/p2pmerge.csv'):
        win32ui.MessageBox('P2PMerge.csv file does exists.\n\nWill continue
with P2P Reporting.')
         win32ui.MessageBox('P2PMerge.csv files does not exists. \n\nPlease
run XXXXXXX.bat files first.')
    win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease
create and copy all the files there.\nThen re-run this script')
fh = open('C://P2P/P2PMerge.csv', "rb")
ff = open('C://P2P/P2PComplete.csv', "wb")
igot1 = fh.readlines()

for line in igot1:
    readline = line
    ipline = readline
    ctline = readline
    count = ctline.split(',')[2]
    count2 = int(count)
    print count2
    t = count2
    ip = ipline.split(' ')[0]
    split_ip = ip.split('.')
    if ((split_ip[0] == '192') and (t >=75)):
        totalcount +=1
    elif ((split_ip[0] == '151') and (t >=75)):
        totalcount +=1
    elif (((split_ip[0] == '142') and (split_ip[1]) == '152') and (t >=75)):
          totalcount +=1

tc = str(totalcount)
win32ui.MessageBox('Total Number of IPs in P2P Reporting: '+ tc)

What I am looking for is an working example of how to go through the
directory and read each csv file within that directory or how to remove the
carriage return at the end of the csv file.

NOTE: This is not for a class - it is for work to assist me in reading
multiple csv files within a couple days.

Any assistance is greatly appreciated.

-- Paras Kinariwala
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090526/6f955794/attachment.htm>

More information about the Tutor mailing list