반응형
pickle.loads
:
TypeError : 'str'은 버퍼 인터페이스를 지원하지 않습니다.
pickle.load
:
TypeError : 파일에는 'read'및 'readline'속성이 있어야합니다.
누군가 가이 과정에서 내가 뭘 잘못하고 있는지 말해 줄 수 있습니까?
elif str(parser) == "SwissWithdrawn_Parser":
# swissprot name changes
print("Gathering SwissProt update info...")
cache_hits = 0
cache_misses = 0
files = set()
for f in os.listdir("out/cache/"):
if os.path.isfile("out/cache/" + f):
files.add(f)
for name in sp_lost_names:
cached = False
url = (
"http://www.uniprot.org/uniprot/?query=mnemonic%3a"
+ name
+ "+active%3ayes&format=tab&columns=entry%20name"
)
hashed_url = str(hash(url))
################### For Testing Only - use cache ##################
if hashed_url in files:
cached = True
cache_hits += 1
content = pickle.loads("out/cache/" + hashed_url) # <-- problematic line
else:
cache_misses += 1
content = urllib.request.urlopen(url)
# get the contents returned from the HTTPResponse object
content_list = [x.decode().strip() for x in content.readlines()]
if not cached:
with open("out/cache/" + hashed_url, "wb") as fp:
pickle.dump(content_list, fp)
####################################################################
# no replacement
if len(content_list) is 0:
change_log["swiss-names"] = {name: "withdrawn"}
# get the new name
else:
new_name = content_list[1]
change_log["swiss-names"] = {name: new_name}
해결 방법
먼저 파일을 읽고 (바이너리 바이트
로) pickle.loads ()
를 사용하거나 열린 파일 개체를 pickle.load ()
명령. 후자가 바람직합니다.
with open('out/cache/' +hashed_url, 'rb') as pickle_file:
content = pickle.load(pickle_file)
두 방법 모두 파일 이름에서 피클로드를 지원하지 않습니다.
참조 페이지 https://stackoverflow.com/questions/18261898
반응형
'파이썬' 카테고리의 다른 글
파이썬 Linux의 PYTHONPATH (0) | 2021.01.10 |
---|---|
파이썬 HTML 페이지 및 내용 다운로드 (0) | 2021.01.10 |
파이썬 폴더의 모든 파일을 여는 방법은 무엇입니까? (0) | 2021.01.10 |
파이썬 Python을 사용한 Quicksort (0) | 2021.01.10 |
파이썬 file.write에서 줄 바꿈을 억제합니다. (0) | 2021.01.10 |
댓글