Hướng dẫn pbcopy python - pbcopy python

Tôi đang nhận được lỗi sau khi cố gắng sử dụng pbcopy để thêm đầu ra vào bảng tạm. Tôi đã thử các biến thể khác nhau của lệnh và vẫn không gặp may. Có ai có bất kỳ ý tưởng mà tôi có thể đã thất bại?

Ảnh chụp màn hình lỗi:

Hướng dẫn pbcopy python - pbcopy python

#!/usr/bin/env python3
# This script creates a secure password using all available key combinations.

import secrets , string, os

def prRed(skk): print("\033[91m {}\033[00m" .format(skk))

chars = string.ascii_letters+string.punctuation+string.digits # Cleaner way of assigning variable

print()
pwd_length = int(input('Enter the length of the desired password: '))

print()
print('[+] ' + 'Your secure password is:')
print()

for n in range(1):
    output = ""
    for i in range(pwd_length):
        next_index = secrets.SystemRandom().randrange(len(chars))
        output = output + chars[next_index]
    prRed(os.system("echo '%s' | pbcopy" % output))
print()

Hướng dẫn pbcopy python - pbcopy python

Amir Shabani

3.5645 Huy hiệu vàng30 Huy hiệu bạc59 Huy hiệu Đồng5 gold badges30 silver badges59 bronze badges

Đã hỏi ngày 25 tháng 5 năm 2019 lúc 3:52May 25, 2019 at 3:52

Hướng dẫn pbcopy python - pbcopy python

3

Viết chuỗi Python vào bảng tạm thông qua pbcopy (OS X)

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

defwrite_to_clipboard(output): write_to_clipboard(output):
importsubprocess subprocess
process=subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE) = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode()) .communicate(output.encode())

Hướng dẫn pbcopy python - pbcopy python

For dictionary objects, test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')
test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')

For dictionary objects, test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')
test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')

'bytes' object has no attribute 'encode'

jq trở thành công cụ "làm tất cả" với các sysadmin. Trong khi jq được dùng để "query JSON" - truy cập dữ liệu trong 1 đoạn JSON thì nó cũng được dùng để kiểm tra syntax của 1 đoạn JSON.

JQ là gì

$ whatis jq

jq (1)               - Command-line JSON processor 
 

 

Nhưng nếu máy đã có python/ruby, hay các ngôn ngữ trang bị sẵn stdlib JSON, sẽ không cần phải cài gì thêm cả:

File bad.json ví dụ, thử dùng mắt thường tìm xem nó sai ở đâu:

{  "name": "@exercism/typescript",  "description": "Exercism exercises in Typescript.",  "private": true,  "repository": {    "type": "git",    "url": "https://github.com/exercism/typescript"  },  "devDependencies": {  },}
  "name": "@exercism/typescript",
  "description": "Exercism exercises in Typescript.",
  "private": true,
  "repository": {
    "type": "git",
    "url": "https://github.com/exercism/typescript"
  },
  "devDependencies": {
  },
}

Cả 3 công cụ sau đều trả về exit code khác 0 (tức không thành công, hay "bị  lỗi")

$ cat bad.json | jq ; echo $?parse error: Expected another key-value pair at line 11, column 14
parse error: Expected another key-value pair at line 11, column 1
4
$ cat bad.json | python3 -m json.tool ; echo $?Expecting property name enclosed in double quotes: line 11 column 1 (char 231)1
Expecting property name enclosed in double quotes: line 11 column 1 (char 231)
1
$ cat bad.json | ruby -rjson -e 'JSON.load(STDIN)' ; echo $?Traceback (most recent call last):        3: from -e:1:in `'        2: from /home/vagrant/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:335:in `load'        1: from /home/vagrant/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:156:in `parse'/home/vagrant/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:156:in `parse': 783: unexpected token at '{ (JSON::ParserError)  "name": "@exercism/typescript",  "description": "Exercism exercises in Typescript.",  "private": true,  "repository": {    "type": "git",    "url": "https://github.com/exercism/typescript"  },  "devDependencies": {  },}'1
Traceback (most recent call last):
        3: from -e:1:in `
'
        2: from /home/vagrant/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:335:in `load'
        1: from /home/vagrant/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:156:in `parse'
/home/vagrant/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:156:in `parse': 783: unexpected token at '{ (JSON::ParserError)
  "name": "@exercism/typescript",
  "description": "Exercism exercises in Typescript.",
  "private": true,
  "repository": {
    "type": "git",
    "url": "https://github.com/exercism/typescript"
  },
  "devDependencies": {
  },
}
'
1


Khi có trong tay những ngôn ngữ lập trình hạng xịn, trang bị tận răng như Python hay Ruby, bạn có thể làm rất rất nhiều thứ, trong 1 2 dòng, mà không cần cài thêm gì. Kiểm tra syntax JSON cũng không phải ngoại lệ.

Happy coding.

Tham khảo 

https://pp.pymi.vn/article/pycli/

Hết.

HVN at "học python tại PyMi" https://pymi.vn and https://www.familug.org