在这个例子中我有3个文本文件,我想把它们转换成csv文件: 我可以对一个文件执行此操作,但对于多个文件,我在多次调整代码后发现错误:
import os
import glob3
count =1
file_name = "COUNT16_DISTRIBUTION" + str(count*1) + ".txt"
def data_parser(text, dic):
for i, j in dic.iteritems():
text = text.replace(i,j)
return text
while count<=3:
for count in file_name:
inputfile = open(file_name)
outputfile = open("COUNT16_DISTRIBUTION" + str(count*1)+ '.csv', 'w')
reps = {'"DUT 1"':' ', ' ':' ', ' ':' ' }
for i in range(7): inputfile.next()
count = count + 1
file_name = "COUNT16_DISTRIBUTION" + str(count * 1) + ".txt"
for line in inputfile:
outputfile.writelines(data_parser(line, reps))
inputfile.close()
outputfile.close()
在这个特殊的例子中,我遇到了问题,因为首先我将count转换为string,然后我想将它作为整数使用,以便递增并检查条件。有其他的想法或方法吗?或者有什么改进的建议吗?在
用于文件计数的变量名
count =1
也用于在for count in file_name:
中遍历文件名。因此,count
将不再是当前文件号,而是采用file_name
的字符。在您只需为for循环使用不同的变量名。例如,
for c in file_name:
相关问题 更多 >
编程相关推荐