[Python] using the list exercises formatted output to a file

After a lot of data collection, data content is often difficult to directly read, for example, the following data
Here Insert Picture Description
is based on a series format in a format arrangement, when we read, needs to be converted to readable form, such as after conversion becomes as follows style
Here Insert Picture Description
ideas:
1. read the text information, and then split added to the list of empty

在这里插入代码片with open('log','r',encoding='utf-8') as l:
    for i in l.readlines():
        li.append(i.strip().split('&'))

2. formatted output to the list

for i in range(len(list1)):
    str = '{} 体重:{} 公斤 跑步速度:{}公里/小时 跑步时间:{}分钟 运动距离:{}公里 燃烧卡路里:{}千卡'.format(list1[i][0], list1[i][1],list1[i][2], list1[i][3],list1[i][4], list1[i][5])
    print(str)

Upgrading Exercise: With the increase of the content of the information over 10, only the last 10 output.
Ideas:
1. The length of the first statistics of the list, and then reverse for loop output, or output directly

list1=[]
with open('log','r',encoding='utf-8') as l:
    for i in l.readlines():
        list1.append(i.strip().split('&'))
num=len(list1)
if num>10:
    for i in range(num-1,num-11,-1):
        str = '{} 体重:{} 公斤 跑步速度:{}公里/小时 跑步时间:{}分钟 运动距离:{}公里 燃烧卡路里:{}千卡'.format(list1[i][0], list1[i][1],list1[i][2], list1[i][3],list1[i][4], list1[i][5])
        print(str)
else:
    for i in range(num-1,-1,-1):
        str = '{} 体重:{} 公斤 跑步速度:{}公里/小时 跑步时间:{}分钟 运动距离:{}公里 燃烧卡路里:{}千卡'.format(list1[i][0], list1[i][1],list1[i][2], list1[i][3],list1[i][4], list1[i][5])
        print(str)
Published 13 original articles · won praise 1 · views 197

Guess you like

Origin blog.csdn.net/aa12551827/article/details/104542283