-2

This Python 3 code:

commentmsg =   '\n' +  svn_date + ' Repo: '+ svn_repository  + ' Rev: ' + svn_revision  + ' User: ' + svn_author + '\n'

gives this result:

b'2020-01-29' b'09:26:49' Repo: SubversionTraining Rev: 478 User: bengt.nilsson

There is a 'b' in front of date and time, where does it come from and how do I get rid of it? This started with Python 3.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • 2
    Does this answer your question? [What does the 'b' character do in front of a string literal?](https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal) – Michele Dorigatti Feb 04 '20 at 08:02
  • Can you give the output of `type(svn_date)` ? – Laurent H. Feb 04 '20 at 08:07

1 Answers1

0

This happens because one of your variables is probably of type bytes instead of str (the b represents a bytes object). Try doing the following:

raw_data = [svn_date, svn_repository, svn_revision, svn_author]
data = [ele.decode() for ele in raw_data if isinstance(ele, bytes) else ele]
commentmsg = f"\n{data[0]} Repo: {data[1]} Rev: {data[2]} User: {data[3]}"
Sam Chats
  • 2,271
  • 1
  • 12
  • 34
  • Actually, `str()` (as well as f-strings) won't work with `bytes` (i.e. it'll call `repr()` under the scenes and still return `b"xxxx"`). You need to `decode()` the bytes to string using an encoding like `utf-8`. – trolley813 Feb 04 '20 at 08:07
  • @trolley813 thanks, I didn't know that. – Sam Chats Feb 04 '20 at 13:34
  • @trolley813 See my edit; will it work now? – Sam Chats Feb 04 '20 at 13:37
  • Actually, the code looks like this: svn_longdate = svnlookup('date', svn_repo_path, svn_revision).split() svn_date = '%s %s' % (svn_longdate[0], svn_longdate[1]) svn_repository = os.path.basename(svn_repo_path) svn_message_line = svn_message.replace('"','\'\'').split('\r\n') commentmsg = '\n' + svn_date + ' Repo: '+ svn_repository + ' Rev: ' + svn_revision + ' User: ' + svn_author + '\n' I tried to put .decode(utf-8) on variables but then I get the message "'list' object has no attribute 'decde'" – Bengt Nilsson Feb 07 '20 at 08:40