I am attempting to take information from each file and use it to create a new file. Each file has one line consisting of a series of numbers. I want each line of one file to line up with each line of the other, then take each number in a line of one file to meet the other number in the same position of the other line in the other file. File 'Volume.txt' has each line shifted one point over (hence the code with k = j+1).
*I keep getting a single Chinese character repeated a lot when the operation is done. So where did I go wrong? Thanks a lot!*
Here is the code:
e = open('C:/Users/MC/Desktop/EODP.txt', 'r')
v = open('C:/Users/MC/Desktop/Z/Volume.txt', 'r')
n = open('C:/Users/MC/Desktop/vovere.txt', 'w')
m = 0 #Used later for letting me know that the first line in the new file completed
for i in range(0, 3256): # there are 3257 lines per the two files EOPD and Volume
l = [] # create a list to put data in during operation
er = e.readline() #
es = er.split(', ') # create a list the data from that line of file
vr = v.readline() #
vs = vr.split(', ') # same
for j in range(len(es)):
k = j + 1 # file Volume is shifted one point ahead
try:
if float(vs[k]) == 0.0:
vovere = 0.0 # vovere is the name of the output for each point
elif vs[k] == '' or vs[k] == ' ':
vovere = 0.0
else:
vovere = float(es[j])/float(vs[k])
except ValueError: #kept getting this error: some points weren't numbers
vovere = 0.0
except IndexError: # Each file didn't always exactly equal in line length
vovere = 0.0
la = l.append(float(vovere)) #Creates the list for each new line in new file
ls = str(la)
l1 = ls.replace('[', '') # Taking away extra notations so the new file is a little
l2 = l1.replace(']', '') # more clean and can be put into other programs
n.write(l2)
if m == 0: # From here on out is just for me, not necessary**
print("The first line is done!")
m += 1
else:
pass
e.close() #** Except these of course
print(e.closed)
print("Yes, EOPD.txt is closed")
v.close()
print(v.closed)
print("Yes, Volume.txt is closed")
n.close()
print(n.closed)
print("Yes, vovere.txt is now ready for further manipulation!!")