【问题标题】:Formatting output file in python在python中格式化输出文件
【发布时间】:2014-03-07 07:41:26
【问题描述】:

我正在尝试像我的输入文件一样格式化我的输出文件。我想知道是否有人可以给我一些指示。我的代码是:

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()

输入文件如下所示:

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

我的输出文件如下所示:

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

它没有正确对齐。

【问题讨论】:

标签: python


【解决方案1】:

你可以这样做:

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()

但我猜你实际上想在打印之前修改行中的某些内容。如果你告诉我什么,我会尽力帮助你。

【讨论】:

  • 我这样做了,但它仍然看起来像那样。我还编辑了我的代码以添加条带并且没有更改。
  • 啊好的,所以问题是对齐,而不是你得到一个额外的\n。上次编辑后更清楚了。您是否需要在打印之前实际修改行中的某些内容?如果是,是什么?
  • 我希望输出文件与输入文件中的完全一样,以便我可以在其中做更多的事情,例如寻找特定的字符串等。如果我的输出文件看起来不像我的输入文件,那么程序的其余部分没有给我正确的答案。
  • 那么您可以查看我的答案的最后编辑,甚至只是复制文件,例如使用shutil.copyfile。如果您想在打印之前修改行中的字段,只需告诉我们什么。
  • 程序创建的输出文件将与输入文件具有相同的格式(相同的字段宽度和间距)。请注意,当用户选择所有行时,输出文件将与输入文件相同。间距信息:国家(50 个字符)收入水平(6 个字符)疫苗接种百分比(3 个字符)地区(25 个字符)年份(4 个字符)
【解决方案2】:

看起来字段宽度是个问题。尝试使用填充:http://docs.python.org/release/3.0.1/whatsnew/2.6.html#pep-3101

这是我的输入示例:

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)

输出:

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

【讨论】:

  • 输入文件很长,有几行。我只是复制并粘贴了几行。所以,我不能使用你的方式,因为它需要我格式化输入文件中的每一行。
  • 您可以修改我的代码以在 format() 函数调用中接受字符串变量。
  • 我还看到您正在尝试使用列表。我不允许使用列表。
【解决方案3】:

此答案对您的问题的回应: How to print a list tuples in a nice format?

使用Python字符串格式:http://docs.python.org/2/library/string.html#format-examples

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多