0

I am trying to format my output file exactly like my input file. I was wondering if anyone could give me some pointers. My codes are:

input_file=open('measles.txt','r')

f1=input('file name: ')

output_file=open(f1,'w')
for line in input_file:
    newstring=''
    line=line.strip()
    for ch in line:
        newstring+=ch

    print(newstring,file=output_file)

input_file.close()
output_file.close()

The input file looks like this:

Afghanistan                                        WB_LI   65 Eastern Mediterranean     2011
Afghanistan                                        WB_LI   68 Eastern Mediterranean     2012
Albania                                            WB_LMI  90 Europe                    1980
Albania                                            WB_LMI  90 Europe                    1981

and my output file is looking like this:

Afghanistan                                        WB_LI   65 Eastern Mediterranean     2011
Afghanistan                                        WB_LI   68 Eastern Mediterranean     2012
Albania                                           WB_LMI   90 Europe                  1980
Albania                                           WB_LMI   90 Europe                  1981

It is not aligned properly.

1

3 Answers 3

1

You could do:

input_file=open('measles.txt','r')

f1 = 'out.txt'
output_file=open(f1,'w')
for line in input_file:
    line=line.rstrip()
    print(line)

input_file.close()
output_file.close()

But I guess you actually want to modify something in the line before you print it. If you tell me what, I will try and help you.

5
  • I did that and it is still looking like that. I also edited my code to add strip and no change.
    – user3260982
    Commented Feb 22, 2014 at 19:26
  • ah ok, so the problem is the alignment, not the fact that you get an extra \n. It's more clear after your last edit. do you need to actually modify something in the lines before printing them? if yes, what? Commented Feb 22, 2014 at 19:31
  • I want the output file exactly like it looks like in the input file so that I can do some more things in it e.g. look for particular strings etc. If my output file does not look like my input one, then the rest of the program is not giving me correct answers.
    – user3260982
    Commented Feb 22, 2014 at 19:37
  • then you can have a look at the last edit to my answer, or even just copy the file, e.g. with shutil.copyfile. If you want to modify a field in the line before printing it, just tell us what. Commented Feb 22, 2014 at 19:39
  • The output file created by the program will have the same format as the input file (same field widths and spacing). Note that when the user selects all lines, the output file will be identical to the input file. Spacing info:Country (50 characters) Income Level (6 characters) Percent Vaccinated (3 characters) Region (25 characters) Year (4 characters)
    – user3260982
    Commented Feb 22, 2014 at 19:44
0

It looks like the field width is the problem. Try using padding: http://docs.python.org/release/3.0.1/whatsnew/2.6.html#pep-3101

Here's my example for your input:

strings = []
fmt = '{0:30} {1:8} {2:4} {3:30} {4:8}'
strings.append(fmt.format("Afghanistan", "WB_LI", 65, "Eastern Mediterranean", 2011))
strings.append(fmt.format("Afghanistan", "WB_LI", 68, "Eastern Mediterranean", 2012))
strings.append(fmt.format("Albania", "WB_LMI", 90, "Europe", 1980))
strings.append(fmt.format("Albania", "WB_LMI", 90, "Europe", 1981))
for str in strings:
    print(str)

Output:

Afghanistan                    WB_LI      65 Eastern Mediterranean              2011
Afghanistan                    WB_LI      68 Eastern Mediterranean              2012
Albania                        WB_LMI     90 Europe                             1980
Albania                        WB_LMI     90 Europe                             1981
3
  • Input file is a long one and has several lines. I just copied and pasted a few lines. So, I can't use your way because it requires me to format each line in the input file.
    – user3260982
    Commented Feb 22, 2014 at 19:55
  • You can modify my code to accept string variables in the format() function call.
    – Kurt
    Commented Feb 22, 2014 at 19:59
  • I also see that you are trying to use list. I am not allowed to use list.
    – user3260982
    Commented Feb 22, 2014 at 20:06
0

This answer response to your question: How to print a list tuples in a nice format?

Use of Python string format: http://docs.python.org/2/library/string.html#format-examples