Hướng dẫn read multiple lines from stdin python - đọc nhiều dòng từ stdin python

Có nhiều vấn đề riêng biệt với mã này:

while run:
    # loop through each line of user input, adding it to buffer
    for line in sys.stdin.readlines():
        if line == 'quit':
            run = False

Đầu tiên, bạn có một vòng lặp bên trong sẽ không hoàn thành cho đến khi tất cả các dòng đã được xử lý, ngay cả khi bạn gõ "bỏ" tại một số điểm. Cài đặt run = False không thoát ra khỏi vòng lặp đó. Thay vì bỏ thuốc ngay khi bạn gõ "bỏ", nó sẽ tiếp tục cho đến khi nó nhìn vào tất cả các dòng, và sau đó bỏ nếu bạn gõ "bỏ" bất cứ lúc nào.

Bạn có thể sửa lỗi này khá dễ dàng bằng cách thêm break sau run = False.


Nhưng, có hoặc không có bản sửa lỗi đó, nếu bạn không gõ "bỏ" trong lần đầu tiên qua vòng lặp bên ngoài, vì bạn đã đọc tất cả đầu vào, không có gì khác để đọc, vì vậy bạn sẽ tiếp tục chạy trống Vòng lặp bên trong lặp đi lặp lại mãi mãi mà bạn không bao giờ có thể thoát ra.

Bạn có một vòng lặp có nghĩa là "Đọc và xử lý tất cả các đầu vào". Bạn muốn làm điều đó chính xác một lần. Vì vậy, vòng lặp bên ngoài nên là gì? Nó không nên là dù sao; Cách để làm điều gì đó một lần là không sử dụng một vòng lặp. Vì vậy, để sửa chữa cái này, hãy loại bỏ run và vòng lặp ____99; Chỉ cần sử dụng vòng bên trong.


Sau đó, nếu bạn nhập "Thoát",

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
0 thực sự sẽ là

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
1, vì

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
2 không tước dòng mới.

Bạn sửa lỗi này bằng cách kiểm tra

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
1 hoặc

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
4Ping các dòng.


Cuối cùng, ngay cả khi bạn khắc phục tất cả những vấn đề này, bạn vẫn chờ đợi mãi mãi trước khi làm bất cứ điều gì.

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
2 Trả về

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
6 của các dòng. Cách duy nhất có thể làm điều đó là bằng cách đọc tất cả các dòng sẽ có trên

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
7. Bạn thậm chí không thể bắt đầu lặp lại cho đến khi bạn đọc tất cả các dòng đó.

Khi đầu vào tiêu chuẩn là một tệp, điều đó xảy ra khi tệp kết thúc, vì vậy nó không quá khủng khiếp. Nhưng khi đầu vào tiêu chuẩn là dấu nhắc lệnh windows, dấu nhắc lệnh không bao giờ kết thúc.* Vì vậy, điều này mất mãi mãi. Bạn không thể bắt đầu xử lý danh sách các dòng, bởi vì phải chờ đợi mãi mãi để chờ đợi danh sách các dòng.

Giải pháp là không sử dụng

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
8. Thực sự, không bao giờ có một lý do chính đáng để gọi

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
8 trên bất cứ điều gì,

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
7 hay không. Bất cứ điều gì

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
2 hoạt động đều đã có một số dòng đầy đủ, giống như

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
6 mà

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
2 sẽ cung cấp cho bạn, ngoại trừ nó là "lười biếng": nó có thể cung cấp cho bạn các dòng một lúc một lần. (Và ngay cả khi bạn thực sự cần danh sách, chỉ cần làm

Copied!

import sys # 👇️ multiline input using stdin.readlines() # 👇️ User must press Ctrl + D (Unix) or Ctrl + Z to exit print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit') user_input = sys.stdin.readlines() # 👇️ get list of lines print(user_input) # 👇️ join the list items into a string print(''.join(user_input))
4 thay vì

Copied!

import sys # 👇️ multiline input using stdin.readlines() # 👇️ User must press Ctrl + D (Unix) or Ctrl + Z to exit print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit') user_input = sys.stdin.readlines() # 👇️ get list of lines print(user_input) # 👇️ join the list items into a string print(''.join(user_input))
5.)

Vì vậy, thay vì

Copied!

import sys # 👇️ multiline input using stdin.readlines() # 👇️ User must press Ctrl + D (Unix) or Ctrl + Z to exit print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit') user_input = sys.stdin.readlines() # 👇️ get list of lines print(user_input) # 👇️ join the list items into a string print(''.join(user_input))
6, chỉ cần thực hiện

Copied!

import sys # 👇️ multiline input using stdin.readlines() # 👇️ User must press Ctrl + D (Unix) or Ctrl + Z to exit print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit') user_input = sys.stdin.readlines() # 👇️ get list of lines print(user_input) # 👇️ join the list items into a string print(''.join(user_input))
7 (hoặc, tốt hơn, thay thế hoàn toàn vòng lặp rõ ràng và sử dụng chuỗi các phép biến đổi lặp, như trong câu trả lời của Mgilson.)


Các bản sửa lỗi Jbernardo, Wing Tang Wong, vv được đề xuất đều chính xác và cần thiết. Lý do không ai trong số họ khắc phục sự cố của bạn là nếu bạn có 4 lỗi và sửa 1, mã của bạn vẫn không hoạt động. Đó chính xác là lý do tại sao "không hoạt động" không phải là thước đo hữu ích của bất cứ điều gì trong lập trình và bạn phải gỡ lỗi những gì thực sự sai khi biết liệu bạn có đang tiến bộ hay không.


* Tôi đã nói dối một chút về

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
7 không bao giờ được hoàn thành. Nếu bạn gõ A Control-Z (bạn có thể hoặc không cần phải theo dõi nó bằng một bản trả lại), thì

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
7 đã hoàn thành. Nhưng nếu nhiệm vụ của bạn là thực hiện thoát ngay khi người dùng gõ "bỏ"

Đầu vào người dùng nhiều dòng trong Python #

Để lấy nhiều dòng đầu vào của người dùng:

  1. Sử dụng vòng lặp

    Copied!

    my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
    0 để lặp lại miễn là người dùng đang nhập các giá trị.
  2. Trên mỗi lần lặp, nối đầu vào người dùng và ký tự mới vào danh sách.
  3. Nếu người dùng nhấn

    Copied!

    my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
    1 mà không cần nhập giá trị, hãy thoát ra khỏi vòng lặp.

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))

Hướng dẫn read multiple lines from stdin python - đọc nhiều dòng từ stdin python

Chúng tôi đã sử dụng vòng lặp

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
0 để lặp lại miễn là người dùng đang gõ các giá trị.

Trên mỗi lần lặp, chúng tôi kiểm tra xem người dùng đã nhấn

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
1 mà không cần nhập giá trị để thoát khỏi vòng lặp

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
4.

Đây có thể là bất kỳ điều kiện nào khác, ví dụ: Bạn có thể có một từ dừng như

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
5 hoặc

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
6.

Tuyên bố phá vỡ thoát ra khỏi vòng lặp

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
7 hoặc

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
0.

Nếu người dùng gõ một giá trị, chúng tôi sử dụng phương thức

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)
9 để nối giá trị và ký tự mới vào danh sách.

Phương thức Danh sách.Append () thêm một mục vào cuối danh sách.

Ngoài ra, bạn có thể sử dụng phương pháp

Copied!

my_list = ['a', 'b', 'c'] my_str = '\n'.join(my_list) # a # b # c print(my_str)
0.

Copied!

import sys # 👇️ multiline input using stdin.readlines() # 👇️ User must press Ctrl + D (Unix) or Ctrl + Z to exit print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit') user_input = sys.stdin.readlines() # 👇️ get list of lines print(user_input) # 👇️ join the list items into a string print(''.join(user_input))

Hướng dẫn read multiple lines from stdin python - đọc nhiều dòng từ stdin python

Copied!

lines = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # 👇️ prints list of strings print(lines) # 👇️ join list into a string print(''.join(lines))
7 được sử dụng cho đầu vào người dùng tương tác.

Lưu ý rằng người dùng phải nhấn

Copied!

my_list = ['a', 'b', 'c'] my_str = '\n'.join(my_list) # a # b # c print(my_str)
2 (trên Unix) hoặc

Copied!

my_list = ['a', 'b', 'c'] my_str = '\n'.join(my_list) # a # b # c print(my_str)
3 trên Windows để thoát.

Nếu bạn không muốn nối một ký tự mới vào từng mục trong danh sách ban đầu, hãy sử dụng phương thức

Copied!

my_list = ['a', 'b', 'c'] my_str = '\n'.join(my_list) # a # b # c print(my_str)
4 để tham gia các mục danh sách vào một chuỗi.

Copied!

my_list = [] while True: user_input = input() # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: my_list.append(user_input) # 👇️ prints list of strings print(my_list) # 👇️ joins list of strings into a string my_str = '\n'.join(my_list) print(my_str)

Hướng dẫn read multiple lines from stdin python - đọc nhiều dòng từ stdin python

Thay vì nối thêm một ký tự Newline (

Copied!

my_list = ['a', 'b', 'c'] my_str = '\n'.join(my_list) # a # b # c print(my_str)
5) vào mỗi đầu vào, chúng tôi đã sử dụng phương thức

Copied!

my_list = ['a', 'b', 'c'] my_str = '\n'.join(my_list) # a # b # c print(my_str)
4 để tham gia danh sách với bộ phân cách ký tự mới.

Phương thức str.join lấy một điều đáng tin cậy như một đối số và trả về một chuỗi là sự kết hợp của các chuỗi trong điều kiện có thể sử dụng được.

Copied!

my_list = ['a', 'b', 'c'] my_str = '\n'.join(my_list) # a # b # c print(my_str)

Chuỗi phương thức được gọi là bật được sử dụng làm phân tách giữa các phần tử.