简体   繁体   English

Python:在 for 循环中使用 end="" 后如何打印新行?

[英]Python : How to print new line after using end="" in for loop?

I was trying to print out the records of customer in list using for loop, but every item in the list was printed out in every new line.我试图使用 for 循环打印出列表中的客户记录,但列表中的每个项目都在每个新行中打印出来。 Therefore, I tried to use end="" to print every item on the same line, but how can I print another customer records on next line whenever the the last column of records is printed out?因此,我尝试使用end=""在同一行打印每个项目,但是当打印出最后一列记录时,如何在下一行打印另一个客户记录?

customer_list = [["ctm1","jaconsdsdsd","ja@gmail.com","+60123954213","15/5/1990"],["ctm2","jactin","jac@gmail.com","+60123237213","15/5/1990"],["ctm3","jactmartin","jacksontai@gmail.com","+60166191111","15/5/1990"]]

# show records of customer account info
def view_customer_account():
     # initialize column length
     column_length = [2,8,13,12,13]
     # loop for longest data length and make it as column length
     # column length will remain as initial column length if longest data length is shorter than it
     for customer in customer_list:
          for i in range(len(column_length)):
               if len(customer[i]) > column_length[i]:
                    column_length[i] = len(customer[i])
     # print column name
     # column name string will concatenate with a space which multiply with (column length - initial column length)
     print(f'| ID{" "*(column_length[0]-2)} | Username{" "*(column_length[1]-8)} | Email Address{" "*(column_length[2]-13)} | Phone number | Date of Birth |')
     # print records
     # records(str) will concatenate with a space which multiply with (column length/initial column length) - records str length
     for customer in customer_list:
          for i in range(len(column_length)):
               print(f'| {customer[i]+" "*(column_length[i]-len(customer[i]))} ',end="")
     
view_customer_account()

Output without using end="" : Output 不使用end=""

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1
| jaconsdsdsd
| ja@gmail.com
| +60123954213
| 15/5/1990
| ctm2
| jactin
| jac@gmail.com        
| +60123237213
| 15/5/1990
| ctm3
| jactmartin
| jacksontai@gmail.com
| +60166191111
| 15/5/1990

Output with end="" : Output with end=""

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | ja@gmail.com         | +60123954213 | 15/5/1990     | ctm2 | jactin      
| jac@gmail.com        | +60123237213 | 15/5/1990     | ctm3 | jactmartin  | jacksontai@gmail.com | +60166191111 | 15/5/1990

Expecting output:期待 output:

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | ja@gmail.com         | +60166197213 | 15/5/1990     |
| ctm2 | jactin      | jac@gmail.com        | +60166197213 | 15/5/1990     |
| ctm3 | jactmartin  | jacksontai@gmail.com | +60166197213 | 15/5/1990     |

Add print('|') after the inner for loop to print the last |在内部for循环之后添加print('|')以打印最后一个| character of each row and insert a new line:每行的字符并插入新行:

    for customer in customer_list:
        for i in range(len(column_length)):
            print(f'| {customer[i] + " " * (column_length[i] - len(customer[i]))} ', end="")
        print('|')  # add this one

Output: Output:

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | ja@gmail.com         | +60123954213 | 15/5/1990     |
| ctm2 | jactin      | jac@gmail.com        | +60123237213 | 15/5/1990     |
| ctm3 | jactmartin  | jacksontai@gmail.com | +60166191111 | 15/5/1990     |

Just add a print() statement (without any parameters) in the outer loop.只需在外循环中添加一个print()语句(不带任何参数)。 One level left of the "innermost" print add the end of your code. “最内层” print左侧的一层添加代码的末尾。

It will just advance the "cursor" to a new line without actually printing anything.它只会将“光标”推进到新行,而不实际打印任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM