【问题标题】:Converting between strings and intergers. Python字符串和整数之间的转换。 Python
【发布时间】:2020-05-28 18:47:20
【问题描述】:

我正在构建一个条形码扫描仪。扫描仪按预期工作。然后我决定将值更改为 ascii 5 的偏移量以提供额外的数据保护。这也按预期工作。为了进一步安全,我希望添加用户输入的密码。我加密码前的原代码如下......

barcodeData = barcode.data.decode("ascii")

barcodeData = "".join(chr(ord(c) +5) for c in barcodeData

然后我决定在第一行添加用户输入

userkey = input()
key=float(userkey)

然后替换

barcodeData = "".join(chr(ord(c) +5) for c in barcodeData

barcodeData = "".join(chr(ord(c) +'key') for c in barcodeData

这会引发错误

TypeError: +: 'int' 和 'str' 的操作数类型不受支持

我希望系统在所有输入下运行,但仅在用户输入数字5时才显示正确的输出

提前致谢

# import the necessary packages
from imutils.video import VideoStream
from pyzbar import pyzbar
import argparse
import datetime
import imutils
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", type=str, default="barcodes.csv",
    help="path to output CSV file containing barcodes")
args = vars(ap.parse_args())

#start video stream and allow warming of camera
print("While camera is warming up, please enter the numerical password.")
vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
userkey=input()
key=str(userkey)


# open the output CSV file for writing and initialize the set of
# QR barcodes found thus far
csv = open(args["output"], "w")
found = set()

# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream and resize it to
    # have a maximum width of 600 pixels
    frame = vs.read()
    frame = imutils.resize(frame, width=600)

    # find the QR Codes in the frame and decode each of the barcodes
    barcodes = pyzbar.decode(frame)

    # loop over the detected barcodes
    for barcode in barcodes:
        # extract the bounding box location of the barcode and draw
        # the bounding box surrounding the barcode on the image
        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3)


        # the barcode data is a bytes object so if we want to draw it
        # on our output image we need to convert it to a string first
        barcodeData = barcode.data.decode("ascii")


        #Chnage the decoded ascii string by a value of 5 charcters
        barcodeData = "".join(chr(ord(c) + 'key') for c in barcodeData)

        # draw the barcode data and barcode type on the image
        text = "{}".format(barcodeData)
        cv2.putText(frame, text, (x, y - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

        # if the barcode text is currently not in our CSV file, write
        # the timestamp + barcode to disk and update the set
        if barcodeData not in found:
            csv.write("{},{}\n".format(datetime.datetime.now(),
                barcodeData))
            csv.flush()
            found.add(barcodeData)

    # show the output frame
    cv2.imshow("QR Code Secret Message Scanner", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

# close the output CSV file nad perform cleanup
csv.close()
cv2.destroyAllWindows()
vs.stop()

【问题讨论】:

  • ord 返回int 数字并且您尝试将其与字符串连接,要修复它只需像str(ord(c)) 一样用str 包装,或提供输入和输出示例以提供帮助你
  • iv 在原始帖子中添加了我的完整代码,如果这是有用的,我的计划是让代码为整数的所有输入运行 - 错误 -barcodeData = "".join(chr( ord(c) + 'key') for c inbarcodeData) TypeError: unsupported operand type(s) for +: 'int' and 'str'
  • 如果您只想连接一个字符串,请尝试将数字值转换为字符串。或者,如果要对值求和,请尝试将字符串值转换为数字。这是你唯一遇到的问题。
  • 试试这个barcodeData = "".join(str(chr(ord(c)) + 'key') for c inbarcodeData)
  • @kwingkwingko,所以这会读取代码,但是它会按原样读取条形码,并且不会通过输入来更改 ascii 值 - 此外 - 每个字符后面都有“key”

标签: python string raspberry-pi int


【解决方案1】:

更改此行(您的行中 ord return int 和 key 有单引号的问题,我认为这是一个错误)

barcodeData = "".join(chr(ord(c) + 'key') for c in barcodeData)

到下一个:

barcodeData = "".join(chr(ord(c) + key) for c in barcodeData)

一个例子:

key = 5 # your offset
barcode = 'AB1234VD'
barcode = "".join(chr(ord(c) + key) for c in barcode)
print(barcode) # 'FG6789[I'

附:不要忘记将代码中的key 转换为int

【讨论】:

    【解决方案2】:

    这里返回的key不是字符串,而是int的数据类型,如果你想转换它,你必须将它类型转换为字符串:

    key = str(userkey)
    

    同样,int 的字符串是:

    key = int(userkey)
    

    【讨论】:

    • 感谢@FishingCode 的回复。我仍然得到与 int() 基数为 10 的无效文字混合的相同错误:'userkey'
    • 你的意思是 int(userkey) 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多