Pavol Bujna
Pavol Bujna

Reputation: 179

How can I split the string variable to multiple variables when the string is a comma-separated?

I'm receiving a string like this sentence "Mr,Pavol,Bujna,has arrived" from a server. To my Raspberry Pi with Python sockets...

It's working well, but need to split the sentence to separate variables.

What I have now:

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

What I need:

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

How can I split the string variable to multiple variables when the string is a comma-separated?

Raspberry Pi code. Important line is:
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)

This code is for the ALPR system I made. Raspberry Pi has an e-ink display where it's showing who arrived. As the display is not long enough, I need to split the sentence into multiple lines, that's why I need multiple variables to work with.

Raspberry Pi with E-ink display

Upvotes: 0

Views: 1152

Answers (2)

Pavol Bujna
Pavol Bujna

Reputation: 179

Thanks, I figured it out myself meanwhile.

Splitting message2 string to multiple variables

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

Whole code:

#!/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

Upvotes: 0

Guru Charan
Guru Charan

Reputation: 11

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

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

or u could use tuple unpacking as well

Upvotes: 1

Related Questions