[英]convert multiple text files to csv
在此示例中,我有 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 =1
的变量名称count =1
也用于循环遍历您的 file_name in for count in file_name:
。 因此, count
将不再是当前文件号,而是采用file_name
的字符。
您需要做的就是为您的 for 循环使用不同的变量名称。 例如, for c in file_name:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.