Hello I am new to python coding and have a couple of things that I could really use some help on. I need to move some sor files from one folder to another and to add a date and time to the facts sheet that it produces if any one could help i would b greatly appriciated. Attached is my code

import csv, datetime

franchiseList = {}

with open('Franchise_Name_Scrub_List.csv', 'r') as ff:
    fcf = csv.DictReader(ff)
    for frow in fcf:
        franchiseList[frow['Misc Franchise Name']] = frow
with open('Franchise_Name_Scrub_List.csv', 'r') as fF:
    fcf = csv.DictReader(fF)
    for Frow in fcf:
        franchiseList[Frow['FRANCHISE Name - Directory']] = Frow

newrow={'Last Sale Date': '', 'Last Sale Amount': '', 'First Name': '', 'Last Name': '', 'Email Address': '', 'Franchise': '', 'State': '', 'Postal/Zip Code': '', 'Last Web Order ID': '', 'Date Added': '', 'Email Source':'', 'osg_web_dir': ''}
updaterow={'Last Sale Date': '', 'Last Sale Amount': '', 'First Name': '', 'Last Name': '', 'Email Address': '', 'Franchise': '', 'State': '', 'Postal/Zip Code': '', 'Last Web Order ID': '',  'osg_web_dir': ''}
new_field_names = newrow.keys()
update_field_names = updaterow.keys()

with open('/Users/HatterX/Desktop/Unprocessed/SOR935csv.csv', 'r') as f1, open('/Users/HatterX/Desktop/Bronto Files/FACTS_bronto_import_add3_24_14.csv', 'a') as f2, open('/Users/HatterX/Desktop/Bronto Files/FACTS_bronto_import_update3_24_14.csv', 'a') as f3:
    cf1 = csv.DictReader(f1, fieldnames=('CustNo1', 'CustNo2', 'LastOrderDate', 'LastOrderAmount', 'FirstName', 'LastName', 'UserNo', 'EmailAddress', 'Franchise', 'PrevOrderDate', 'PrevOrderAmount', 'State', 'ZIP', 'Amt1', 'Amt2', 'Amt3', 'SalesPerson', 'WEBID'))
    cf2 = csv.DictWriter(f2, new_field_names)
    cf3 = csv.DictReader(f1, fieldnames=('CustNo1', 'CustNo2', 'LastOrderDate', 'LastOrderAmount', 'FirstName', 'LastName', 'UserNo', 'EmailAddress', 'Franchise', 'PrevOrderDate', 'PrevOrderAmount', 'State', 'ZIP', 'Amt1', 'Amt2', 'Amt3', 'SalesPerson', 'WEBID'))
    cf4 = csv.DictWriter(f3, update_field_names)
    cf2.writeheader()
    cf4.writeheader()

    for row in cf1:
        nr = newrow
        nr['Last Sale Date'] = row['LastOrderDate'].strip()
        nr['Last Sale Amount'] = row['LastOrderAmount'].strip()
        nr['First Name'] = row['FirstName'].strip()
        nr['Last Name'] = row['LastName'].strip()
        nr['Email Address'] = row['EmailAddress'].strip().split(',',1)[0]
        if nr['Email Address'] == '':
            continue

        fr_name = row['Franchise'].strip()
        if fr_name in franchiseList:
                    nr['Franchise'] = franchiseList[fr_name]['FRANCHISE Name'].strip()
                    if nr['Franchise'] == 'IGNORE':
                        continue
                    nr['osg_web_dir'] = franchiseList[fr_name]['FRANCHISE Name - Directory'].strip()
                    if nr['osg_web_dir'] == '':
                        nr['osg_web_dir'] = 'shop'
        else:
                    nr['Franchise'] = 'SHOP'
                    nr['osg_web_dir'] = 'shop'

        nr['State'] = row['State'].strip()
        nr['Postal/Zip Code'] = row['ZIP'].strip()
        nr['Last Web Order ID'] = row['WEBID'].strip()
        nr['Date Added'] = datetime.date.today().strftime('%m/%d/%Y')  
        nr['Email Source'] = 'FACTSauto'
        print nr
        cf2.writerow(nr)


        ur = updaterow
        ur['Last Sale Date'] = row['LastOrderDate'].strip()
        ur['Last Sale Amount'] = row['LastOrderAmount'].strip()
        ur['First Name'] = row['FirstName'].strip()
        ur['Last Name'] = row['LastName'].strip()
        ur['Email Address'] = row['EmailAddress'].strip().split(',',1)[0]
        if ur['Email Address'] == '':
                    continue

        fr_name = row['Franchise'].strip()
        if fr_name in franchiseList:
                    ur['Franchise'] = franchiseList[fr_name]['FRANCHISE Name'].strip()
                    if ur['Franchise'] == 'IGNORE':
                        continue
                    ur['osg_web_dir'] = franchiseList[fr_name]['FRANCHISE Name - Directory'].strip()
                    if ur['osg_web_dir'] == '':
                        ur['osg_web_dir'] = 'shop'
        else:
                    ur['Franchise'] = 'SHOP'
                    ur['osg_web_dir'] = 'shop'

        ur['State'] = row['State'].strip()
        ur['Postal/Zip Code'] = row['ZIP'].strip()
        ur['Last Web Order ID'] = row['WEBID'].strip()
        print ur
        cf4.writerow(ur)

Recommended Answers

All 2 Replies

I do not understand what you want to ask, your code is reasonable, except I do not recommend to repeat the field names instead of simple assignment using copy method to make fresh copy of dictionary.

You may also have chicken and egg problem where to get the first file to read at beginning.

What i am wanting it to do is on the add and update file i want it to add the current date and time to the end of it and with the file that is being read i ahve several of those that need to be read and moved from one folder to another on my desktop automatically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.