【发布时间】:2015-01-20 05:42:17
【问题描述】:
我遇到的问题是将 phone_sorter() 和 number_calls() 都打印在同一行上。例如,它将打印 phone_sorter 的两行,但 number_calls 将打印在它的正下方。我已经尝试过 end='' 方法,但它似乎不起作用。
customers=open('customers.txt','r')
calls=open('calls.txt.','r')
def main():
print("+--------------+------------------------------+---+---------+--------+")
print("| Phone number | Name | # |Duration | Due |")
print("+--------------+------------------------------+---+---------+--------+")
print(phone_sorter(), number_calls())
def time(x):
m, s = divmod(seconds, x)
h, m = divmod(m, x)
return "%d:%02d:%02d" % (h, m, s)
def phone_sorter():
sorted_no={}
for line in customers:
rows=line.split(";")
sorted_no[rows[1]]=rows[0]
for value in sorted(sorted_no.values()):
for key in sorted_no.keys():
if sorted_no[key] == value:
print(sorted_no[key],key)
def number_calls():
no_calls={}
for line in calls:
rows=line.split(";")
if rows[1] not in no_calls:
no_calls[rows[1]]=1
else:
no_calls[rows[1]]+=1
s={}
s=sorted(no_calls.keys())
for key in s:
print(no_calls[key])
main()
【问题讨论】:
-
在Python2.7中,只需要以逗号结尾即可。
print 'output', -
这是python3,我已经试过逗号的方法了。
-
@Kirill 你在哪里使用
end=''? -
如果
print('output', end="")没有这样做,我不知道该告诉你什么。 -
在 phone_sorter 和 number_calls 的最后一次打印中,它不起作用,所以我将其删除。
标签: python printing formatting