/ / Získajte binárny súbor z gitpythonu podľa revízie (dostal som unicodestring, chcel bajtov) - git, python-3.x, gitpython

Získajte binárny súbor z gitpythonu podľa revízie (dostal unicodestring, chce bajty) - git, python-3.x, gitpython

Chcem získať prístup k obsahu binárneho súboru v repozitári git pomocou gitpython, bohužiaľ repo.git.show vráti reťazec unicode a nie objekt bajtov. Takže chcem previesť reťazec na bajty a nedarí sa to.

#!/usr/bin/env python

from io import BytesIO
import git

# initialize repository
repo = git.Repo(".")
# use git show to get the content of example.jpg in revision 19e91a
u = repo.git.show("4cb2a02:example.jpg")

b = BytesIO(u.encode("utf-8"))

a naraziť

UnicodeEncodeError: "utf-8" codec can"t encode character "udcff" in position 0: surrogates not allowed

To nie je prekvapenie.

Ako môžem konvertovať tento unicode reťazec na bajty? Alebo lepšie, ako môžem načítať obsah súboru ako bajtový objekt?

odpovede:

1 pre odpoveď č. 1

vyskúšať

b = BytesIO(u.encode("utf-8","surrogateescape"))