본문 바로가기

binarymode2

Python requests를 사용하여 이미지를 다운로드하는 방법은 다음과 같습니다., How to download image using requests 질문 나는 파이썬의 requests 모듈을 사용하여 웹에서 이미지를 다운로드하고 저장하려고 시도하고 있습니다. 다음은 사용한 (작동하는) 코드입니다: img = urllib2.urlopen(settings.STATICMAP_URL.format(**data)) with open(path, 'w') as f: f.write(img.read()) requests를 사용한 새로운 (작동하지 않는) 코드는 다음과 같습니다: r = requests.get(settings.STATICMAP_URL.format(**data)) if r.status_code == 200: img = r.raw.read() with open(path, 'w') as f: f.write(img) requests에서 응답으로부터 사용할 속성.. 2023. 10. 17.
Python 파이썬 3에서 파일 내용을 처리할 때 'str'이 아닌 바이트 유사 객체가 필요합니다(TypeError: a bytes-like object is required, not 'str')., "TypeError: a bytes-like object is required, not 'str'" when handling file conten.. 질문 나는 매우 최근에 Python 3.5로 이전했다. 이 코드는 Python 2.7에서 제대로 작동했다: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp: continue # ... code 하지만 3.5에서 if 'some-pattern' in tmp: continue 라인에서 오류가 발생하여 다음과 같이 말합니다: TypeError: a bytes-like object is required, not 'str' 나는 in의 양쪽에 .decode()를 사용하여 문제를 해결할 수 없었으며, if tm.. 2023. 6. 27.