0

I am following a Machine Learning tutorial (from a text book) and I got stuck on a portion of the code I was given and I am not sure how to proceed. It seems as though I need to change the data type of object to a "bytes-like" as opposed to a string.

I have tried adding a line changing "name" to encode utf-8

for line in data:
    if firstLine:
        names = line.strip().split(";")
        firstLine = False
    else:
    ...

The error I receive:

TypeError: a bytes-like object is required, not 'str'

I am trying to understand why the data-type must be changed, and how I can change it.

DYZ
  • 55,249
  • 10
  • 64
  • 93
Pythoner
  • 460
  • 6
  • 23
  • 5
    You haven't provided us enough detail to answer why the data-type must be changed, as this depends on the library and API you are using. To convert a `str` to `bytes` use [`str.encode()`](https://docs.python.org/3/library/stdtypes.html?highlight=encode#str.encode), if you are already doing this then perhaps you have mistaken where your error is - provide the exception details. – AChampion Jan 08 '19 at 01:17
  • Possible duplicate of [Best way to convert string to bytes in Python 3?](https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) – iz_ Jan 08 '19 at 01:19
  • 1
    Let me guess, python 2.7? Reading from a csv? Those questions usually start like this. – Daniel Scott Jan 08 '19 at 01:20
  • Sorry! First time poster here. I am using Python3 on Spyder. And yes, I am trying to read from a CSV – Pythoner Jan 08 '19 at 01:28
  • 1
    Please include the _complete_ error message. – DYZ Jan 08 '19 at 01:35
  • 1
    You need to provide a [mcve]. Are you opening your file in binary mode? – juanpa.arrivillaga Jan 08 '19 at 01:38
  • The error message implies that `names` is a byte string, which means the parameter to `split` must also be a byte string: `b";"`. – Mark Ransom Jan 08 '19 at 04:01

0 Answers0