在字符串和整数之间转换。python

2024-05-16 11:01:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作条形码扫描仪。扫描仪按预期工作。然后,我决定将该值更改为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: unsupported operand types(s) for +: 'int' and '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()

Tags: andofthetokeyinimportfor
2条回答

此处返回的key不是字符串,而是int的数据类型,如果要将其转换,必须通过执行以下操作将其类型转换为字符串:

key = str(userkey)

同样,int的字符串是:

key = int(userkey)

更改这一行(行中的问题是ord返回int和key有单引号,我假设这是一个bug)

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

相关问题 更多 >