I have made a script, with python 3, that downloads single files from github repositories.
It uses requests, but when it saves to a file it has the format
b'text'
I need to get rid of the b (which is always the first character of the line), but I can't just use .replace as that gets rid of all bs.
If anyone has any has any idea as how to replace the first character of the file (b) and are willing to help, thank you.
Asked
Active
Viewed 179 times
0
-
3No, you don't need to "get rid of b", you need to convert your `bytes` object to a `str` object. See first answer to https://stackoverflow.com/questions/606191/convert-bytes-to-a-string – Błotosmętek Apr 12 '20 at 16:09
-
2Your script should be writing data, not Python representations of the data, to the file. – chepner Apr 12 '20 at 16:10
-
Use .decode() mthod to convert it into strings. default is "utf-8" – GraphicalDot Apr 12 '20 at 16:10
-
You forgot to include a [mcve] so we can only guess what you did wrong. But you are doing it wrong. – Jongware Apr 12 '20 at 16:25
1 Answers
3
You have confused that 'b' for a character. See here What does the 'b' character do in front of a string literal?
To get rid of it, you need to decode it
url = "https://projecteuler.net/project/resources/p067_triangle.txt"
content = requests.get(url).content.decode('utf8')

dgg32
- 1,409
- 1
- 13
- 33
-
1Or let requests do the decoding: `content = requests.get(url).text` - this might be better if the encoding isn't known in advance. – snakecharmerb Apr 13 '20 at 08:38