【发布时间】:2017-12-27 03:19:07
【问题描述】:
我在此示例中有 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()
在这种特殊情况下,现在我遇到了问题,因为首先我将计数转换为字符串,然后我想将其用作整数以递增并检查条件。任何其他想法或方法或任何改进这种方式的建议?
【问题讨论】:
-
for count in file_name将遍历file_name的字母并覆盖或屏蔽count。 -
为 for 循环使用不同的变量名。
count将采用 file_name 中字母的值。