簡體   English   中英

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

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

我試圖使用 for 循環打印出列表中的客戶記錄,但列表中的每個項目都在每個新行中打印出來。 因此,我嘗試使用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 不使用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=""

| 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:

| 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     |

在內部for循環之后添加print('|')以打印最后一個| 每行的字符並插入新行:

    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:

| 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     |

只需在外循環中添加一個print()語句(不帶任何參數)。 “最內層” print左側的一層添加代碼的末尾。

它只會將“光標”推進到新行,而不實際打印任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM