DEV Community

Zephaniah Joshua
Zephaniah Joshua

Posted on

Parsing Data

Alt Text

Yesterday I wrote about the difference between UDP and TCP. Today we will be talking about sending information that contains different data types over a network by parsing.

A string of data can easily be sent over a network because it can easily be encoded as bytes but the same cannot be said for a large amount of data that contains different data types e.g strings, integers, float, tuples, dictionaries etc.

For example, if we have data that contains a string, date, a tuple as information we need to send over a network we might need to encompass all this data into a single message convert to bytes and send over the network. On receiving the data the server will separate the data back into its individual components and decode it.
The process of separating data back to its components is called parsing.

  1. First step is converting other data types to string e.g Converting an integer to a string str(123)
  2. Encompassing converted data into a single variable with a separator like “,” to separate the different data.
data1 = “hello”
data2 = 123

#convert the integer to string
data2 = str(data2)
#encompassing data into single variable
message = data1 + “,” data2
Enter fullscreen mode Exit fullscreen mode
  1. Encode the message message.encode() and send socket.send(msg).

The server has received the message and will now parse it

#recieve and decode message
message = socket.recv(1024)
msg = message.decode()

#split the message using the split function in python
#remember that we used “, ” to separate data
parts = msg.split(“,”)
#data1 is a string
data1 = str(parts[0])
#data2 is an int
data2 = int(parts[1])

Enter fullscreen mode Exit fullscreen mode

So basically parsing is changing all data to be sent into a uniform data type(in our case a string) sending it as a single message and on reception separating it back to its components and returning them to there original data types.

Hopes this gives you a basic understanding of parsing. Feel free to ask questions or leave suggestions in the comment section or contact me @black_strok3.

image

Top comments (0)