字符串以逗号分隔时,如何将字符串变量拆分为多个变量?

时间:2020-09-02 15:32:33

标签: python python-3.x raspberry-pi

我正在从服务器收到这样的字符串,“先生,Pavol,Bujna,已经到了” 。 到带有Python套接字的Raspberry Pi ...

效果很好,但需要将句子拆分为单独的变量。

我现在所拥有的:

message2 = 'Mr,Pavol,Bujna,has arrived'

我需要什么:

firstname = 'Pavol'
surname = 'Bujna'
arravingLeaving = 'has arrived'

当字符串以逗号分隔时,如何将字符串变量拆分为多个变量?

Raspberry Pi代码。 重要的一行是:
draw.text((10, 40), message2, font = font20, fill = 0)

#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
    sys.path.append(libdir)

import logging
from waveshare_epd import epd2in13d
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )


#Set output log level
logging.basicConfig(level=logging.DEBUG)

while True:
    (data,addr) = mySocket.recvfrom(SIZE)
    message = str(data) #make string from data
    message2 = message[2:-1] #remove first (b), second (') and last (') character
    #try:
    logging.info("epd2in13d Demo")

    epd = epd2in13d.EPD()
    logging.info("init and Clear")
    epd.init()
    epd.Clear(0xFF)

    font15 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15)
    font20 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20)
    font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)

    # Drawing on the Horizontal image
    logging.info("1.Drawing on the Horizontal image...")
    Himage = Image.new('1', (epd.height, epd.width), 255)  # 255: clear the frame
    draw = ImageDraw.Draw(Himage)

    draw.text((150, 0), time.strftime('%H:%M'), font = font20, fill = 0)
    draw.text((10, 40), message2, font = font20, fill = 0)


    epd.display(epd.getbuffer(Himage))
    time.sleep(2)

此代码用于我制作的ALPR系统。 Raspberry Pi具有电子墨水显示屏,用于显示谁到达。 由于显示时间不够长,我需要将句子分成多行,这就是为什么我需要使用多个变量的原因。

Raspberry Pi with E-ink display

2 个答案:

答案 0 :(得分:1)

谢谢,我自己想出来了。

将message2字符串拆分为多个变量

    title, firstName, lastName, arravingLeaving = message2.split(",")
    print(title)
    print(firstName)
    print(lastName)
    print(arravingLeaving)

整个代码:

#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
    sys.path.append(libdir)

import logging
from waveshare_epd import epd2in13d
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )


#Set output log level
logging.basicConfig(level=logging.DEBUG)

while True:
    (data,addr) = mySocket.recvfrom(SIZE)
    message = str(data) #make string from data
    message2 = message[2:-1] #remove first (b), second (') and last (') character
    
    #Splitting message2 string to multiple variables
    title, firstName, lastName, arravingLeaving = message2.split(",")
    print(title)
    print(firstName)
    print(lastName)
    print(arravingLeaving)

    #try:
    logging.info("epd2in13d Demo")

    epd = epd2in13d.EPD()
    logging.info("init and Clear")
    epd.init()
    epd.Clear(0xFF)

    font15 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15)
    font20 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20)
    font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)

    # Drawing on the Horizontal image
    logging.info("1.Drawing on the Horizontal image...")
    Himage = Image.new('1', (epd.height, epd.width), 255)  # 255: clear the frame
    draw = ImageDraw.Draw(Himage)

    draw.text((150, 0), time.strftime('%H:%M'), font = font20, fill = 0)
    draw.text((15, 0), title, font = font20, fill = 0)
    draw.text((10, 25), firstName, font = font20, fill = 0)
    draw.text((10, 50), lastName, font = font20, fill = 0)
    draw.text((10, 75), arravingLeaving, font = font20, fill = 0)


    epd.display(epd.getbuffer(Himage))
    time.sleep(2)

Working prototype

答案 1 :(得分:1)

message2 ='先生,Pavol,Bujna,已经到达'

words=message2.split(',')
firstname=words[1]
lastname=words[2]
arravingLeaving=words[3]

或者您也可以使用元组拆包