Hướng dẫn python argument string - chuỗi đối số python

6

Nội dung chính ShowShow

  • Làm thế nào để bạn lấy một chuỗi trong một đối số trong Python?
  • Làm thế nào để bạn lấy chuỗi trong một cuộc tranh luận?
  • Làm thế nào để bạn viết các lập luận trong Python?
  • Làm thế nào để bạn khai báo một biến chuỗi trong Python?

Nội dung chính ShowShowShow

  • Làm thế nào để bạn lấy một chuỗi trong một đối số trong Python?
  • Làm thế nào để bạn lấy chuỗi trong một cuộc tranh luận?
  • Làm thế nào để bạn viết các lập luận trong Python?
  • Làm thế nào để bạn khai báo một biến chuỗi trong Python?

Nội dung chính ShowShow
Learn more.

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.Learn more.

python3 myprogram.py afile.json

Tôi cần phải lấy một đối số tùy chọn khi chạy tập lệnh Python của mình:

python3 myprogram.py

hoặc

filename = 0
parser = argparse.ArgumentParser(description='Create Configuration')
parser.add_argument('filename', type=str,
                   help='optional filename')

if filename is not 0:
    json_data = open(filename).read()
else:
    json_data = open('defaultFile.json').read()

Đây là những gì tôi đã thử:

Hướng dẫn python argument string - chuỗi đối số python

Tôi đã hy vọng có tên tệp được lưu trữ trong biến của tôi có tên là "Tên tệp" nếu nó được cung cấp. Rõ ràng điều này không hoạt động. Bất kỳ lời khuyên?

Martineau25 gold badges160 silver badges285 bronze badges

Huy hiệu vàng 116K2525 gold badges160 silver badges285 bronze badges25 gold badges160 silver badges285 bronze badgesOct 3, 2012 at 18:18

Đã hỏi ngày 3 tháng 10 năm 2012 lúc 18:18Oct 3, 2012 at 18:18Oct 3, 2012 at 18:18

Vui lòng đọc kỹ hướng dẫn. http://docs.python.org/howto/argparse.html

parser = argparse.ArgumentParser()
args = parser.parse_args()

Tôi tin rằng bạn cần thực sự phân tích các lập luận:

python3 myprogram.py
3

Sau đó, tên tệp sẽ có sẵn Oct 3, 2012 at 18:22

dm03514dm03514dm03514dm03514dm03514

Đã trả lời ngày 3 tháng 10 năm 2012 lúc 18:22Oct 3, 2012 at 18:22Oct 3, 2012 at 18:2218 gold badges104 silver badges142 bronze badges

53.4K18 Huy hiệu vàng104 Huy hiệu bạc142 Huy hiệu đồng18 gold badges104 silver badges142 bronze badges18 gold badges104 silver badges142 bronze badges

Kiểm tra sys.argv. Nó đưa ra một danh sách với tên của tập lệnh và bất kỳ đối số dòng lệnh nào.

import sys
print sys.argv

Kịch bản ví dụ:

> python script.py foobar baz
['script.py', 'foobar', 'baz']

Sau đó, tên tệp sẽ có sẵn Oct 3, 2012 at 18:22

Đã trả lời ngày 3 tháng 10 năm 2012 lúc 18:22Oct 3, 2012 at 18:22Oct 3, 2012 at 18:22Junuxx

53.4K18 Huy hiệu vàng104 Huy hiệu bạc142 Huy hiệu đồng18 gold badges104 silver badges142 bronze badges18 gold badges104 silver badges142 bronze badges5 gold badges40 silver badges68 bronze badges

Kiểm tra sys.argv. Nó đưa ra một danh sách với tên của tập lệnh và bất kỳ đối số dòng lệnh nào.

Sau đó, tên tệp sẽ có sẵn Oct 3, 2012 at 18:22

0

Đã trả lời ngày 3 tháng 10 năm 2012 lúc 18:22Oct 3, 2012 at 18:22Oct 3, 2012 at 18:22

import argparse

parser = argparse.ArgumentParser(description='Create Configuration')
parser.add_argument('--file-name', type=str, help='optional filename', 
       default="defaultFile.json")

args = parser.parse_args()

print(args.file_name)

Output:

$ python open.py --file-name option1
option1

$ python open.py 
defaultFile.json

53.4K18 Huy hiệu vàng104 Huy hiệu bạc142 Huy hiệu đồng18 gold badges104 silver badges142 bronze badges18 gold badges104 silver badges142 bronze badges:

Kiểm tra sys.argv. Nó đưa ra một danh sách với tên của tập lệnh và bất kỳ đối số dòng lệnh nào.

Kịch bản ví dụ:Feb 23 at 20:31

nishnet2002nishnet2002nishnet2002nishnet2002nishnet2002

Gọi nó:1 gold badge2 silver badges9 bronze badges

53.4K18 Huy hiệu vàng104 Huy hiệu bạc142 Huy hiệu đồng18 gold badges104 silver badges142 bronze badges18 gold badges104 silver badges142 bronze badges

Kiểm tra sys.argv. Nó đưa ra một danh sách với tên của tập lệnh và bất kỳ đối số dòng lệnh nào.how to pass the strong value to the function in Python?
Submitted by IncludeHelp, on September 06, 2018

Kịch bản ví dụ:how to pass string value to the function?

Kịch bản ví dụ:

Input:
str = "Hello world"

Function call:
printMsg(str)

Output:
"Hello world"

Program:

python3 myprogram.py
0

Gọi nó:

python3 myprogram.py
0

53.4K18 Huy hiệu vàng104 Huy hiệu bạc142 Huy hiệu đồng18 gold badges104 silver badges142 bronze badgesJunuxx

python3 myprogram.py
1

Gọi nó:

python3 myprogram.py
2

53.4K18 Huy hiệu vàng104 Huy hiệu bạc142 Huy hiệu đồng18 gold badges104 silver badges142 bronze badgesJunuxx



Làm thế nào để bạn lấy một chuỗi trong một đối số trong Python?

Kiểm tra sys.argv. Nó đưa ra một danh sách với tên của tập lệnh và bất kỳ đối số dòng lệnh nào.5 gold badges40 silver badges68 bronze badgesdef printMsg(str): # printing the parameter print str # Main code # function calls printMsg("Hello world!") printMsg("Hi! I am good.")

Làm thế nào để bạn lấy chuỗi trong một cuộc tranh luận?

53.4K18 Huy hiệu vàng104 Huy hiệu bạc142 Huy hiệu đồng18 gold badges104 silver badges142 bronze badgeswrite the name of the string array variable. In the following example we have a string array variable message and it is passed to the displayString function.

Làm thế nào để bạn viết các lập luận trong Python?

Thí dụ:. You can add as many arguments as you want, just separate them with a comma.. You can add as many arguments as you want, just separate them with a comma.

Làm thế nào để bạn khai báo một biến chuỗi trong Python?

Đầu rausing the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value. The quotes around the number 12 signify that the number is no longer an integer but is now a string value.using the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value. The quotes around the number 12 signify that the number is no longer an integer but is now a string value.