转换为多个文本cs文件

2024-04-27 00:41:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在这个例子中我有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,然后我想将它作为整数使用,以便递增并检查条件。有其他的想法或方法吗?或者有什么改进的建议吗?在


Tags: 文件csvtextnameinimportforcount
1条回答
网友
1楼 · 发布于 2024-04-27 00:41:58

用于文件计数的变量名count =1也用于在for count in file_name:中遍历文件名。因此,count将不再是当前文件号,而是采用file_name的字符。在

您只需为for循环使用不同的变量名。例如,for c in file_name:

相关问题 更多 >