Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I have been running a content where I utilize the ord() function and for whatever the explanation in python 2.7, it acknowledges the Unicode string character similarly as it requires and yields a whole number. 

In python 3.4, this isn't so much the situation. This is the output of the error that is being delivered : 

Traceback (most recent call last):

  File "udpTransfer.py", line 38, in <module>

    buf.append(ord(c))

TypeError: ord() expected string of length 1, but int found

At the point when I look in the two documentations, the ord work is disclosed to do a similar careful thing. 

This is the code that I am utilizing for both python forms: 

import socket,sys, ast , os, struct

from time import ctime

import time

import csv

# creating the udo socket necessary to receive data

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

ip = '192.168.10.101' #i.p. of our computer

port = 20000 # socket port opened to connect from the matlab udp send data stream

server_address = (ip, port)

sock.bind(server_address)   # bind socket

sock.settimeout(2)          # sock configuration

sock.setblocking(1)

print('able to bind')

ii = 0

shotNummer = 0

client = ''

Array = []

byte = 8192

filename = time.strftime("%d_%m_%Y_%H-%M-%S")

filename = filename + '.csv'

try :

    with open(filename,'wb') as csvfile :

        spamwriter = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)

        # spamwriter.writerow((titles))

        # as long as data comes in, well take it

        while True:

            data,client = sock.recvfrom(byte)

            buf = []

            values = []

            for c in data:

                # print(type(c))

                buf.append(ord(c))

                if len(buf) == 4 :

                    ###

Would anyone be able to clarify why python3.4 says that c is a whole number, instead of in Python 2.7 where it is really a string, similarly as the ord() work requires? 

1 Answer

0 votes
by (26.4k points)

I think the thing that matters is that in Python 3 the sock.recvfrom(...) call returns bytes while Python 2.7 recvfrom restores a string. So ord didn't change however what is being passed to ord has changed.

Want to become an expert in python? Come and join python certification course.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...