Hướng dẫn how do you store output in python? - làm thế nào để bạn lưu trữ đầu ra trong python?

Tôi mới sử dụng Python và tôi tự hỏi làm thế nào để lưu trữ đầu vào hoặc đầu ra trong một biến.

Trong Shell-script, tôi có thể sử dụng điều này để lưu trữ đầu ra của lệnh trong một biến

Variable_name=$(Command) 

Sau đó, tôi có thể sử dụng nó như thế này chẳng hạn: ví dụ:

start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do

Đây là mã Python của tôi tất nhiên không hoạt động:

first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)

Tôi đã đến đây một bài tập khác mà tôi đã viết:

first = int(input("Enter first number: "))
second = int(input("Enter second number: "))


def adding(first, second):
        return(first + second)

result = adding(first, second)

print(result)

def plustest(result):
        return(result + 100)

result2 = plustest(result + 100)
print(result2)

Điều tôi không thích là tôi phải viết "Thêm (thứ nhất, thứ hai):" Hai lần thay vì lấy nó từ def.

def adding(first, second):
        return(first + second)

result = adding(first, second)
print(result)

giống

result="def adding(first, second):
        return(first + second)"
print($result) 

hoặc

result=$(def adding(first, second):
        return(first + second))
print($result) 

Tôi không muốn phát minh lại Python nhưng tôi tự hỏi làm thế nào để đạt được những gì tôi có thể dễ dàng làm trong script script trong Python. Ngoài ra, nếu bất cứ ai có thể đoán được ngôn ngữ nào sẽ đến gần với sự pha trộn giữa Shell-script và Python. Tôi đã từng dùng chữ ký và tôi thích sử dụng ống, grep, seg, dán, v.v. nhưng tôi thiếu những lợi thế của ngôn ngữ lập trình thực sự. Hy vọng nó có ý nghĩa.

Cheers.

💬 Câu hỏi: Làm thế nào để chuyển hướng đầu ra tiêu chuẩn trong Python và lưu trữ nó dưới dạng chuỗi trong một biến?Question: How to redirect the standard output in Python and store it as a string in a variable?

Bài viết này sẽ hướng dẫn bạn qua bảy bước dễ dàng để giải quyết vấn đề này. Như một cái nhìn tổng quan, ở đây, mã trong tám dòng lưu trữ đầu ra tiêu chuẩn trong một biến

start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
5:

  1. start_num=$(input start number) 
    increment_num=$(input increment number) 
    
    i=$start_num;
    while [[ i -le $increment_num ]] ;
      do
    
    6
  2. start_num=$(input start number) 
    increment_num=$(input increment number) 
    
    i=$start_num;
    while [[ i -le $increment_num ]] ;
      do
    
    7
  3. start_num=$(input start number) 
    increment_num=$(input increment number) 
    
    i=$start_num;
    while [[ i -le $increment_num ]] ;
      do
    
    8
  4. start_num=$(input start number) 
    increment_num=$(input increment number) 
    
    i=$start_num;
    while [[ i -le $increment_num ]] ;
      do
    
    9
  5. first = int(input("Enter the starting number: "))
    second = int(input("Increment it by: "))
    
    def start(first):
            return(first)
    
    def increment(second):
            return(second)
    
    count = start
    while count != 100:
            count += increment
            print(count)
    
    0
  6. first = int(input("Enter the starting number: "))
    second = int(input("Increment it by: "))
    
    def start(first):
            return(first)
    
    def increment(second):
            return(second)
    
    count = start
    while count != 100:
            count += increment
            print(count)
    
    1
  7. first = int(input("Enter the starting number: "))
    second = int(input("Increment it by: "))
    
    def start(first):
            return(first)
    
    def increment(second):
            return(second)
    
    count = start
    while count != 100:
            count += increment
            print(count)
    
    2
  8. first = int(input("Enter the starting number: "))
    second = int(input("Increment it by: "))
    
    def start(first):
            return(first)
    
    def increment(second):
            return(second)
    
    count = start
    while count != 100:
            count += increment
            print(count)
    
    3

Hãy cùng đi qua các bước đó từng bước một, chúng tôi sẽ kiểm tra mã đầy đủ để sao chép & dán vào cuối bài viết này, vì vậy hãy đọc tiếp! 👀we’ll examine the full code for copy&paste at the end of this article, so read on! 👀

Bước 1: Nhập thư viện Stringio và SYS

Nhập hai thư viện

first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
4 và
first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
5 để truy cập vào đầu ra tiêu chuẩn và lưu trữ luồng đầu vào chuỗi đầu vào.

from io import StringIO 
import sys

Cả hai mô -đun là một phần của thư viện tiêu chuẩn, vì vậy không cần phải cài đặt chúng với PIP!

Bước 2: Giữ stdout trong biến tạm thời

Chúng tôi sẽ ghi đè lên đầu ra tiêu chuẩn để bắt mọi thứ được ghi vào nó. Để đặt lại mã của bạn về trạng thái bình thường, chúng tôi cần nắm bắt luồng đầu ra tiêu chuẩn ban đầu bằng cách giới thiệu một biến tạm thời.

tmp = sys.stdout

Bước 3: Chụp đầu ra tiêu chuẩn bằng cách sử dụng đối tượng Stringio

Tạo một biến và gán một đối tượng

first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
4 cho biến để chụp luồng đầu ra tiêu chuẩn.

my_result = StringIO()

Bây giờ, đối tượng này có thể lưu trữ mọi thứ được in vào đầu ra tiêu chuẩn. Nhưng chúng ta phải kết nối nó trước với stdout!

Bước 4: Gán luồng đầu ra tiêu chuẩn cho đối tượng Stringio

Gán đối tượng

first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
4 được tạo trong bước trước cho đầu ra tiêu chuẩn được chụp bằng
first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
8.

start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
0

Bước 5: In vào đầu ra tiêu chuẩn

Từ thời điểm này trở đi, bất cứ điều gì được in bằng câu lệnh

first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
9 bởi bất kỳ chức năng nào bạn gọi trong tập lệnh Python của bạn đều được viết trong đối tượng
first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
4 được đề cập bởi biến
start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
5.

Sau đây minh họa cho tuyên bố

first = int(input("Enter first number: "))
second = int(input("Enter second number: "))


def adding(first, second):
        return(first + second)

result = adding(first, second)

print(result)

def plustest(result):
        return(result + 100)

result2 = plustest(result + 100)
print(result2)
2 nhưng bạn có thể làm bất cứ điều gì ở đây:

start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
1

💡 Lưu ý: Không có đầu ra nào xuất hiện trên màn hình nữa vì đầu ra tiêu chuẩn hiện được chuyển hướng đến biến.Note: No output appears on the screen anymore because the standard output is now redirected to the variable.

Bước 6: Dọn dẹp bằng cách chuyển hướng Stdout sang Python Shell

Bạn đã sẵn sàng với việc nắm bắt đầu ra trong biến? Làm sạch bằng cách chuyển hướng luồng đầu ra tiêu chuẩn từ biến sang màn hình một lần nữa.

start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
2

Bước 7: Nhận và in chuỗi từ stdout

Tại thời điểm này, chuỗi của bạn từ đầu ra tiêu chuẩn được lưu trữ trong đối tượng

first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))

def start(first):
        return(first)

def increment(second):
        return(second)

count = start
while count != 100:
        count += increment
        print(count)
4 trong biến
start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
5. Bạn có thể truy cập nó bằng phương pháp
first = int(input("Enter first number: "))
second = int(input("Enter second number: "))


def adding(first, second):
        return(first + second)

result = adding(first, second)

print(result)

def plustest(result):
        return(result + 100)

result2 = plustest(result + 100)
print(result2)
5.

start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
3

Mã đầy đủ

Tại đây, một đoạn mã đầy đủ để dễ dàng sao chép & dán:

start_num=$(input start number) 
increment_num=$(input increment number) 

i=$start_num;
while [[ i -le $increment_num ]] ;
  do
4

Bạn cũng có thể kiểm tra điều này trên máy tính xách tay Jupyter tương tác của chúng tôi để bạn không phải thử trong vỏ của riêng mình:

Hướng dẫn how do you store output in python? - làm thế nào để bạn lưu trữ đầu ra trong python?

🧩 Hãy tự mình thử: Bấm để chạy trong Notebook Jupyter (Google Colab)Try it yourself: Click to run in Jupyter Notebook (Google Colab)

Người giới thiệu:

  • https://wrongsideofmemphis.com/2010/03/01/store-standard-output-on-a-variable-in-python/

Bạn đã sẵn sàng để tăng kỹ năng trăn của mình chưa?Tham gia Học viện Email miễn phí của chúng tôi - chúng tôi cũng đã gian lận các tờ!🙂

Hướng dẫn how do you store output in python? - làm thế nào để bạn lưu trữ đầu ra trong python?

Trong khi làm việc như một nhà nghiên cứu trong các hệ thống phân tán, Tiến sĩ Christian Mayer đã tìm thấy tình yêu của mình đối với việc dạy các sinh viên khoa học máy tính.

Để giúp học sinh đạt được thành công cao hơn của Python, ông đã thành lập trang web giáo dục chương trình Finxter.com.Ông là tác giả của cuốn sách lập trình phổ biến Python Oneer (Nostarch 2020), đồng tác giả của loạt sách Break Break Python, những cuốn sách tự xuất bản, người đam mê khoa học máy tính, freelancer và chủ sở hữu của một trong 10 blog Python lớn nhất trên toàn thế giới.

Niềm đam mê của ông là viết, đọc và mã hóa.Nhưng niềm đam mê lớn nhất của anh là phục vụ các lập trình viên đầy tham vọng thông qua Finxter và giúp họ tăng cường các kỹ năng của họ.Bạn có thể tham gia học viện email miễn phí của anh ấy ở đây.