I have 3 text files in this example and would like to convert them to csv files : I can do it for one file but for multiple files i am aseeing errors after tweaking the code multiple times :
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()
In this particular case now i am running into problem because first i converted count to string and later i want to use it as integer in order to increment and check the condition. Any other thoughts or way to do it or any suggestions to improve this way ?
The variable name you are using for the count of files, count =1
is also being used to loop through your file_name in for count in file_name:
. Because of this, count
will no longer be the current file number, but instead take on the characters of file_name
.
All you need to do is use a different variable name for your for loop. For example, for c in file_name:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.