简体   繁体   English

如何逐个复制输入文件并在python中使用格式?

[英]How to copy input file ch by ch and use format in python?

I have been trying to copy the input file character by character and then trying to format them to avoid any kind of alignment issue. 我一直试图逐个字符地复制输入文件,然后尝试格式化它们以避免任何类型的对齐问题。 The first item in the input file is 50 characters long, the second item is 6 characters long, third is 3 characters long, fourth is 25 characters long, and the fifth is 4 characters long. 输入文件中的第一项为50个字符长,第二项为6个字符长,第三个为3个字符长,第四个为25个字符长,第五个为4个字符长。 I am not allowed to use list or tuple. 我不允许使用列表或元组。 The following is my code: 以下是我的代码:

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:
        #I am trying to format the first two items in the input file
        print('{0:50}{51:57}'.format(line[0:50],line[51:57]),file=output_file)   

input_file.close()
output_file.close()

and the error I am getting is the following: 和我得到的错误如下:

Traceback (most recent call last):
  File "C:/Users/Dasinator/Documents/Books IX/Python Examples/proj07.py", line 10, in <module>
    print('{0:50}{1:57}'.format(line[0:50],line[51:57]),file=output_file)
IndexError: tuple index out of range

Could anyone point out the error in the code and recommend ways to fix it? 谁能指出代码中的错误并提出解决方法的建议? Thanks 谢谢

Andorra                                           WB_HI                                             
Andorra                                           WB_HI                                             
Angola                                           WB_LMI                                            
Angola                                           WB_LMI                                            
Angola                                           WB_LMI   

The output still is not looking perfectly aligned. 输出仍然看起来不完全对齐。

This is how I modified my code and still having some trouble. 这就是我修改代码后仍然遇到麻烦的方式。

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)

and the output that I am getting is 我得到的输出是

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            

Looks like you've misunderstood how .format works, and what the formatting string should look like. 您似乎误解了.format的工作原理以及格式化字符串的外观。

When you write a string " {0:50}{1:50}".format(stuff0, stuff1) the colon separates the index (before), from the number of characters that the string should take up when printed. 当您编写字符串“ {0:50}{1:50}".format(stuff0, stuff1) ,冒号将索引(之前)与打印时字符串应占用的字符数分开。

So, when you do: 因此,当您这样做时:

print('{0:50}{51:57}'.format(line[0:50],line[51:57]),file=output_file)

you're telling python to use the 51st argument to .format. 您告诉python使用第51个参数指定.format。 What you actually mean is the second argument to .format, or the 51st character in the string. 您实际上的意思是.format的第二个参数,即字符串中的第51个字符。

Presumably, what you actually want to do here is print the 0th-50th characters of the line, and the 51st-57th characters. 大概,您实际上想要在这里执行的是打印该行的第0-50个字符以及第51-57个字符。 That will look like: 看起来像:

print('{0:10}{1:10}'.format(line[0:50],line[51:57]),file=output_file)

Additionally, I don't know what you mean the file argument to do, but it's probably not going to do what you expect. 另外,我不知道您要执行的文件参数是什么意思,但可能不会执行您期望的事情。

Why 51 here in second param: {0:50}{51:57} ?, should be 1 : 为什么在第二个参数{0:50}{51:57} 51应该为1

print('{0:50}{1:57}'.format(line[0:50],line[51:57]),file=output_file)
#            ^^^

As test: 作为测试:

>>> print('{0:10}{57:10}'.format(10,20))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> print('{0:10}{1:10}'.format(10,20))
        10        20

To align second column to left side, use < char before width: 要将第二列与左侧对齐,请在宽度前使用< char:

print('{0:50}{1:<57}'.format(line[0:50],line[51:57]),file=output_file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM