I have a block of code that i reads a file and then converts it to a add and update file and im looking to change it to where insead of it having a hard copy created every time for it to use stringio if it is possible. Sence im new to python still im reading over the python webpages and they dont explain what i need to do. If someone could help me i would be geartly appriciative. Below is the code that reads and converts.
os.chdir(fileLoc)
for FILE in glob.glob("SOR935*"):
filepresent = os.path.isfile('/home/hatterx/Desktop/Bronto_Files/FACTS_bronto_import_add_'+dt+'.csv')
with open(FILE, 'r') as f1, open('/home/hatterx/Desktop/Bronto_Files/FACTS_bronto_import_add_'+dt+'.csv', 'ab') as f2, open('/home/hatterx/Desktop/Bronto_Files/FACTS_bronto_import_update_'+dt+'.csv', 'ab') 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)
if False == filepresent:
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'] = row['EmailAddress'].strip().split(',',1)[0]
if nr['Email'] == '':
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'
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'] = row['EmailAddress'].strip().split(',',1)[0]
if ur['Email'] == '':
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()
cf4.writerow(ur)
shutil.move(FILE, "/home/hatterx/Desktop/Processed/"+ dt +"_"+ FILE)