running into a stump, i have two legacy text files that i want to pull data from in order to create one csv file.
to keep things short here is my code exactly as it sits on my screen:
import csv, itertools
list1 = []
with open('D:/py_files/legacy_temp/REPORT_1.TXT', 'rb') as tf:
for line in tf:
if len(line) > 2:
if line[17].isdigit():
acctnum = str(line[16:31])
custname = str(line[39:58])
currbal = str(line[84:96])
diffbal = str(line[102:114])
list1.append(acctnum + '|' + custname + '|' + currbal + '|' + diffbal)
list2 = []
with open('D:/py_files/legacy_temp/REPORT_2.TXT', 'rb') as tf2:
for line in tf2:
if line[0].isdigit():
acctnum = str(line[1:12])
ourbal = str(line[80:90])
alldnum = str(line[123:131])
clntnum = str(line[132:152])
list2.append(acctnum + '|' + ourbal + '|' + alldnum + '|' + clntnum)
the code below is just my scrapbook, things i was trying. i can create the csv file, but it either writes as one long continuous row, or it writes while appending a '|' after each char ie: a|b|c|d| etc...
#mlist = []
#if len(list1) == len(list2):
# for i, j in map(None,list1,list2):
# print i + '|' + j
def f1():
clist = []
outfile = csv.writer(open('D:/py_files/legacy_temp_/report_diff.csv', 'wb'))
if len(list1) == len(list2):
for i, j in map(None,list1,list2):
clist.append(str(i + '|' + j + '\n'))
outfile.writerow(clist)
print '\n'.join(clist)
def f2():
for x,y in zip(list1,list2):
print list1+list2
def f3():
output = list(itertools.chain(list1,list2))
print '\n'.join(output)
two things, a) am i going about this the right way (opening both text files separately), and b)if i am, how can i write a csv file that will give me the following rows:
acctnum|custname|currbal|diffbal|acctnum|ourbal|alldnum|clntnum
with each element within the | above, in a separate cell..
PS. i only used pipe as a delimiter because the balances had commas in them. i do not need to use pipes as i can replace the commas in the balances.
all help is greatly appreciated, thanks