格式化输出到外部txt文件(formatted output to an external txt file)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
格式化输出到外部txt文件(formatted output to an external txt file) fp = open ('data.txt','r') saveto = open('backup.txt','w') someline = fp.readline() savemodfile = '' while someline : temp_array = someline.split() print('temp_array[1] {0:20} temp_array[0] {0:20}'.format(temp_array[1], temp_array[0]), '\trating:', temp_array[len(temp_array)-1])) someline = fp.readline() savemodfile = temp_array[1] + ' ' + temp_array[0] +',\t\trating:'+ temp_array[10] saveto.write(savemodfile + '\n') fp.close() saveto.close()

输入文件:data.txt具有此模式的记录:名字姓氏年龄地址

我希望backup.txt具有以下格式:姓氏名字地址年龄

如何将数据以不错的格式存储在backup.txt中? 我想我应该使用format()方法......

我使用代码中的打印对象向你展示了我对format()的理解。 当然,我没有得到理想的结果。

fp = open ('data.txt','r') saveto = open('backup.txt','w') someline = fp.readline() savemodfile = '' while someline : temp_array = someline.split() print('temp_array[1] {0:20} temp_array[0] {0:20}'.format(temp_array[1], temp_array[0]), '\trating:', temp_array[len(temp_array)-1])) someline = fp.readline() savemodfile = temp_array[1] + ' ' + temp_array[0] +',\t\trating:'+ temp_array[10] saveto.write(savemodfile + '\n') fp.close() saveto.close()

The input file :data.txt has records of this pattern: firstname Lastname age address

I would like the backup.txt to has this format: Lastname firstname address age

How do i store the data in the backup.txt in a nice formatted way? I think i should use format() method somehow...

I use the print object in the code to show you what i understood about format() so far. Of course, i do not get the desired results.

最满意答案

要回答您的问题:您确实可以在字符串模板上使用.format()方法,请参阅文档https://docs.python.org/3.5/library/stdtypes.html#str.format

例如:

'the first parameter is {}, the second parameter is {}, the third one is {}'.format("this one", "that one", "there")

将输出: 'the first parameter is this one, the second parameter is that one, the third one is there'

'temp_array[1] {0:20} temp_array[0] {0:20}'.format(temp_array[1], temp_array[0])会输出一些东西'temp_array[1] {0:20} temp_array[0] {0:20}'.format(temp_array[1], temp_array[0])例如: 'temp_array[1] {0:20} temp_array[0] {0:20}'.format(temp_array[1], temp_array[0])像'temp_array[1] Lastname temp_array[0] Lastname ' 。 这是因为{0:20}会将第一个参数输出到format() ,右边用空格填充为20个字符。

另外,代码中还有许多需要改进的地方。 我想你正在学Python,所以这很正常。 这是一个功能相同的代码,可以生成您想要的输出,并充分利用Python功能和语法:

with open('data.txt', 'rt') as finput, \ open('backup.txt','wt') as foutput: for line in finput: firstname, lastname, age, address = line.strip().split() foutput.write("{} {} {} {}\n".format(lastname, firstname, address, age)

To answer your question: you can indeed use the .format() method on a string template, see the documentation https://docs.python.org/3.5/library/stdtypes.html#str.format

For example:

'the first parameter is {}, the second parameter is {}, the third one is {}'.format("this one", "that one", "there")

Will output: 'the first parameter is this one, the second parameter is that one, the third one is there'

You do not seem to use format() properly in your case: 'temp_array[1] {0:20} temp_array[0] {0:20}'.format(temp_array[1], temp_array[0]) will output something like 'temp_array[1] Lastname temp_array[0] Lastname '. That is because {0:20} will output the 1st parameter to format(), right padded with spaces to 20 characters.

Additionally, there is many things to be improved in your code. I guess you are learning Python so that's normal. Here is a functionally equivalent code that produces the output you want, and makes good use of Python features and syntax:

with open('data.txt', 'rt') as finput, \ open('backup.txt','wt') as foutput: for line in finput: firstname, lastname, age, address = line.strip().split() foutput.write("{} {} {} {}\n".format(lastname, firstname, address, age)

更多推荐

本文发布于:2023-04-15 03:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a65abc35d71a4390e2251c2175d74ef8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   formatted   txt   file   external

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!