我正在尝试从每个文件中获取信息并使用它来创建一个新文件。每个文件都有一行,由一系列数字组成。我希望一个文件的每一行与另一个文件的每一行对齐,然后将一个文件的一行中的每个数字与另一个文件中另一行的同一位置的另一个数字相遇。文件“Volume.txt”的每一行都移动了一个点(因此代码 k = j+1)。
*操作完成后,我不断收到一个汉字重复很多次。那么我哪里做错了?非常感谢!*
这是代码:
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!!")