I'm trying to write a script that will take an input file with an unknown number of columns separated by commas and create a new file (name specified by user) where columns are separated by tabs.
The test input file I'm working with looks like this:
Data 1,35,42,7.34,yellow,male
Data 2,41,46,8.45,red,female
Here is the code I have so far:
# Read input file
infile = open("input_file.txt", "r")
line_count = 0
# Read as a collection, removing end line character
for line in infile:
print(line, end = "")
print("The input file contains", line_count, "lines.")
# Request user input for output file name
filename = input("Enter a name for the output file: ")
# Prompt for file name if entry is blank or only a space
while filename.isspace() or len(filename) == 0:
filename = input("Whoops, try again. Enter a name for the output file: ")
# Complete filename creation
filename = filename + ".txt"
filename = filename.strip()
# Write output as tab-delim file
for line in infile:
outfile = open(filename, "w")
outfile.write(line,"\t")
outfile.close()
print("Success, the file", filename, "has been written.")
# Close input file
infile.close()
The part that writes the output isn't working - it doesn't produce an error, but the output is blank.