0

I am trying to format my output file with the following data. Country (50 characters) Income Level (6 characters) Percent Vaccinated (3 characters) Region (25 characters) Year (4 characters) Here is my code:

f1=input('file name: ')

output_file=open(f1,'w')
for line in input_file:
    newstring=''
    line=line.strip()
    for ch in line:
        print('{0:<50s}{1:<6s}{2:<3s}{3:<25s}{4:<4s}'.format(line[0:50],line[50:56],line[56:59],line[59:84],line[84:87],file=output_file)   

input_file.close()
output_file.close()

Here is what I am getting as error:

file name: output_file.txt
Traceback (most recent call last):
  File "C:/Users/Dasinator/Documents/Books IX/Python Examples/proj07.py", line 10, in <module>
    print('{0:<50s}{1:<6s}{2:<3s}{3:<25s}{4:<4s}'.format(line[0:50],line[50:56]),line[56:59],line[59:84],line[84:87],file=output_file)
IndexError: tuple index out of range

I have been working on this thing for quite some time now and just don't know where I am going wrong. Could anyone please write where the problem lies? Thanks

5
  • unrelated: for ch in line: print(..) calls print for each character in the line. Are you sure this is your intent? Commented Feb 22, 2014 at 22:33
  • This is a very easy coding thing, but the input file is creating all the trouble. So, what I am trying to do is to copy each character from the input file to the output file. Hoping to keep the format the same. But, i realized that in the process, I am losing my year which is the 5th item in the input file. I have been working on this seemingly easy problem for quite some time now and still have no idea how to fix it. Commented Feb 22, 2014 at 23:11
  • "to copy each character from the input file to the output file", you could use import shutil; shutil.copy('input_filename', 'output_path') Commented Feb 22, 2014 at 23:18
  • We haven't used shutil yet. Is there anyway, we can do it ch by ch and still be able to maintain the exact format of the input file? Even if I do the for loop with line like for line in input_file, my output file is not looking like the input file. Commented Feb 22, 2014 at 23:26
  • just look at the source code of shutil.copy*() functions. Commented Feb 22, 2014 at 23:32

1 Answer 1

1

You have added a ) after the second argument in the format function. Then you continue to add arguments, which python is considering part of the print function, which doesn't take so many arguments.

Along with this, the format function is now expecting 6 arguments but you only supplied 2, hence the error

Sign up to request clarification or add additional context in comments.

5 Comments

how to modify it then?
@cobra Get rid of the ) after line[50:56]
I got it. Did the changes and now the output file is coming out nicely. Thanks for looking in my code.
For some reason, my fifth item is not even showing up. Do you know what might be the reason?
@cobra it could be an empty string as a result from your file that you're reading from

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.