0

Here is my code:

d = each_data[0].strip().split("\x0a")

works as expected in Windows, but in Linux it throws error below:

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

each_data[0] contains two lines

Any clue is appreciated, thank you.

Update: Please refer to the script here: https://raw.githubusercontent.com/mdivk/solr_demo/master/scripts/script_p2.py

Environment requirement: python 2.6.6 (sorry I Know this is a low version, it is the built-in python we have on all the CentOS with Cloudera CDH installed, it would be un-realistic to upgrade python in a short time and I need this be fixed in shortest time)

Keep in mind: the code is working as expected in Windows in python 2.7

Choix
  • 555
  • 1
  • 12
  • 28
  • 1
    please provide a minimal runnable example that reproduces your issue and include a full stack trace of the error. – Mad Physicist Apr 21 '18 at 03:14
  • on windows you are running python 2.7 ... on linux you are running 3.x i think – Joran Beasley Apr 21 '18 at 03:20
  • Wrong, on Windows it is 2.7, on Linux it is 2.6.6 – Choix Apr 21 '18 at 11:03
  • each_data[0] is not what you think it is. Check how it is created. Also, the "bytes-like object is required" is an error from Python 3.x *it doesn't exist in python 2.x*, so you'll have to revisit your assertion about what python version you're running this on – Irmen de Jong Apr 21 '18 at 11:27
  • Thank you so much for catching it!!! I have both 2 and 3 installed on the host, and was screwed up by the versions, now the script is working! – Choix Apr 21 '18 at 11:51

2 Answers2

0

Looks like this one is a near-duplicate of: python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file

Try writing split(b"\x0a") (i.e. with a b before the "\x0a"). Or, perhaps you have opened the file as a binary file. Try opening it in text mode.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • No, it is not the same case, it works in 3.5 but I need it to run in 2.6.6 which is the built-in python version of our current CDH version. and adding "b" doesn't work. Thanks. – Choix Apr 21 '18 at 11:01
0

I have both 2 and 3 installed on the host, and was screwed up by the versions, with the right version be selected to run the script, it is working!

Thank you all for the enlightening.

Choix
  • 555
  • 1
  • 12
  • 28