原代码如下,之后是我写入该文档的操作。
import numpy as np
import codecs
import time
if __name__ == '__main__':
allCount = 0
time = 0
lastnum = -1
lists = []
linenum = 0
#init numpy array
with open('B48W21 .txt', 'r') as f:
content = f.read()
for x in content.splitlines():
x = x.split(",")
lists.append(x)
a = np.array(lists)
[rows, cols] = a.shape
#open file
fzt = open("zerotime.txt", "w")
fot = open("onetime.txt", "w")
fz = open("zero.txt", "w")
fo = open("one.txt", "w")
fzm = open("zeromeans.txt", "w")
fom = open("onemeans.txt", "w")
我改后代码如下,但无法运行错误为:
File "", line 15
for ifile in file:
^
SyntaxError: invalid character in identifier
代码为
import numpy as np
import codecs
import time
import os
readfile='F:\\FCD\\实验2'
writefile='F:\\FCD\\实验副本2'
file=os.listdir(readfile)
if __name__ == '__main__':
allCount = 0
time = 0
lastnum = -1
lists = []
linenum = 0
for ifile in file:
#init numpy array
with open(readfile+'\\'+ifile, 'r') as f:
content = f.read()
for x in content.splitlines():
x = x.split(",")
lists.append(x)
a = np.array(lists)
[rows, cols] = a.shape
#open file
fzt = open(writefile+'\\'+'zerotime'+ifile, "w")
fot = open(writefile+'\\'+'onetime'+ifile, "w")
fz = open(writefile+'\\'+'zero'+ifile, "w")
fo = open(writefile+'\\'+'one'+ifile, "w")
fzm = open(writefile+'\\'+'zeromean'+ifile, "w")
fom = open(writefile+'\\'+'onemean'+ifile, "w")
还是说我的思路有问题?对已有文档进行操作的程序,同一文件夹下所有文档进行此操作,不能仅仅修改读写文件,还有其他需要更改的地方?