我的任务是从文本文件中读取数据,但以某种方式格式化它。我必须在单独的部分打印数据。有没有办法剥离使用readlines()的列表?
# read data from DOB.txt file
# separate certain parts of the data
# output should expectations:
# file.txt data -- Chuck Jones 12 December 1997
# Name:
# 1.Chuck Jones
# ect.
# Birht:
# 1.December, 12 1997
# ect.
#lines = []
num = 0
file = open('DOB.txt','r')
print("Name: ")
for line in file:
values = line.split()
num += 1
print(str(num) + str(values[0:2]))
file.close()
答案 0 :(得分:0)
好吧,我已经找到了代码..我唯一的问题是从列表中删除括号并输出输出。
file = open('DOB.txt','r')
num = 0
print("Name: ")
for line in file:
values = line.split()
num += 1
print(str(num) + ".) " + str(values[0:2]))
file.close()
file = open('DOB.txt','r')
num = 0
print("Birth: ")
for line in file:
values = line.split()
num += 1
print(str(num) + ".) " + str(values[3:5]))
file.close()