2

I'm new to python and studying deep learning using Github codes. I'm getting error TypeError: can't concat str to bytes so I tried to match the datatype.

old:

newFilename = fileGenre+"_"+str(fileID)

new:

newFilename = fileGenre+"_"+bytes(fileID)

but same error occured, so I want to know how to fix the error. fileGenre is byte varible which is from eyed3 module and fileID is int type variable.

this is part of code:

    for index,filename in enumerate(files):
       fileGenre = getGenre(rawDataPath+filename)
       genresID[fileGenre] = genresID[fileGenre] + 1 if fileGenre in genresID else 1
       fileID = genresID[fileGenre]
       newFilename = fileGenre+"_"+str(fileID) #in this line, i got error
       createSpectrogram(filename,newFilename)
ddne
  • 23
  • 3
  • Does this answer your question? [Convert bytes to a string](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – mkrieger1 Apr 18 '20 at 09:29

3 Answers3

3

You can only concat str to str so when you are trying to concat str and bytes this will raise an error. You can use bytes.decode method to return a string decodes from given bytes.

Use:

newFilename = fileGenre.decode("utf-8") + "_" + str(fileID)
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
1

Use:

new_filename = "{}_{}".format(fileGenre, str(file_id))

1

you can not use string between two bytes value concatenation ("_")

so you can use this -

newFilename = str(fileGenre)+""+str(fileID) newFilename = fileGenre+""+str(bytes(fileID))

A D
  • 26
  • 2