質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

2540閲覧

write() argument must be str, not bytes を解決したい

jyohonoob

総合スコア7

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/07/25 08:40

write() argument must be str, not bytes を解決したい

興味のある論文の公開されているソースコードを実行しようとしたところwrite() argument must be str, not bytes のエラーに悩まされており,解決できそうにないため,お力を貸していただきたいです.

あまり,新しい論文ではないため,python2系で書かれていたコードをpython3で書き換えている途中です.

ぜひ,よろしくお願いします.

以下がそのコードです。

発生している問題・エラーメッセージ

TypeError Traceback (most recent call last) <ipython-input-20-4999cc12e722> in <module> 78 f = open('../data/FB15k_entity2idx.pkl', 'w') 79 g = open('../data/FB15k_idx2entity.pkl', 'w') ---> 80 cPickle.dump(entity2idx, f, -1) 81 cPickle.dump(idx2entity, g, -1) 82 f.close() TypeError: write() argument must be str, not bytes

該当のソースコード

python

1import os, sys 2#import cPickle 3import _pickle as cPickle 4 5import numpy as np 6import scipy.sparse as sp 7 8# Put the freebase15k data absolute path here 9datapath = "D:/Downloads/dataset/" 10assert datapath is not None 11 12if 'data' not in os.listdir('../'): 13 os.mkdir('../data') 14 15 16def parseline(line): 17 lhs, rel, rhs = line.split('\t') 18 lhs = lhs.split(' ') 19 rhs = rhs.split(' ') 20 rel = rel.split(' ') 21 return lhs, rel, rhs 22 23################################################# 24### Creation of the entities/indices dictionnaries 25 26np.random.seed(753) 27 28entleftlist = [] 29entrightlist = [] 30rellist = [] 31 32for datatyp in ['train']: 33 f = open(datapath + 'freebase_mtr100_mte100-%s.txt' % datatyp, 'r') 34 dat = f.readlines() 35 f.close() 36 for i in dat: 37 lhs, rel, rhs = parseline(i[:-1]) 38 entleftlist += [lhs[0]] 39 entrightlist += [rhs[0]] 40 rellist += [rel[0]] 41 42entleftset = np.sort(list(set(entleftlist) - set(entrightlist))) 43entsharedset = np.sort(list(set(entleftlist) & set(entrightlist))) 44entrightset = np.sort(list(set(entrightlist) - set(entleftlist))) 45relset = np.sort(list(set(rellist))) 46 47entity2idx = {} 48idx2entity = {} 49 50 51# we keep the entities specific to one side of the triplets contiguous 52idx = 0 53for i in entrightset: 54 entity2idx[i] = idx 55 idx2entity[idx] = i 56 idx += 1 57nbright = idx 58for i in entsharedset: 59 entity2idx[i] = idx 60 idx2entity[idx] = i 61 idx += 1 62nbshared = idx - nbright 63for i in entleftset: 64 entity2idx[i] = idx 65 idx2entity[idx] = i 66 idx += 1 67nbleft = idx - (nbshared + nbright) 68 69print ("# of only_left/shared/only_right entities: ", nbleft, '/', nbshared, '/', nbright) 70# add relations at the end of the dictionary 71for i in relset: 72 entity2idx[i] = idx 73 idx2entity[idx] = i 74 idx += 1 75nbrel = idx - (nbright + nbshared + nbleft) 76print ("Number of relations: ", nbrel) 77 78f = open('../data/FB15k_entity2idx.pkl', 'w') 79g = open('../data/FB15k_idx2entity.pkl', 'w') 80cPickle.dump(entity2idx, f, -1) 81cPickle.dump(idx2entity, g, -1) 82f.close() 83g.close() 84 85################################################# 86### Creation of the dataset files 87 88unseen_ents=[] 89remove_tst_ex=[] 90 91for datatyp in ['train', 'valid', 'test']: 92 print (datatyp) 93 f = open(datapath + 'freebase_mtr100_mte100-%s.txt' % datatyp, 'r') 94 dat = f.readlines() 95 f.close() 96 97 # Declare the dataset variables 98 inpl = sp.lil_matrix((np.max(entity2idx.values()) + 1, len(dat)), 99 dtype='float32') 100 inpr = sp.lil_matrix((np.max(entity2idx.values()) + 1, len(dat)), 101 dtype='float32') 102 inpo = sp.lil_matrix((np.max(entity2idx.values()) + 1, len(dat)), 103 dtype='float32') 104 # Fill the sparse matrices 105 ct = 0 106 for i in dat: 107 lhs, rel, rhs = parseline(i[:-1]) 108 if lhs[0] in entity2idx and rhs[0] in entity2idx and rel[0] in entity2idx: 109 inpl[entity2idx[lhs[0]], ct] = 1 110 inpr[entity2idx[rhs[0]], ct] = 1 111 inpo[entity2idx[rel[0]], ct] = 1 112 ct += 1 113 else: 114 if lhs[0] in entity2idx: 115 unseen_ents+=[lhs[0]] 116 if rel[0] in entity2idx: 117 unseen_ents+=[rel[0]] 118 if rhs[0] in entity2idx: 119 unseen_ents+=[rhs[0]] 120 remove_tst_ex+=[i[:-1]] 121 122 # Save the datasets 123 if 'data' not in os.listdir('../'): 124 os.mkdir('../data') 125 f = open('../data/FB15k-%s-lhs.pkl' % datatyp, 'w') 126 g = open('../data/FB15k-%s-rhs.pkl' % datatyp, 'w') 127 h = open('../data/FB15k-%s-rel.pkl' % datatyp, 'w') 128 cPickle.dump(inpl.tocsr(), f, -1) 129 cPickle.dump(inpr.tocsr(), g, -1) 130 cPickle.dump(inpo.tocsr(), h, -1) 131 f.close() 132 g.close() 133 h.close() 134 135unseen_ents=list(set(unseen_ents)) 136print (len(unseen_ents)) 137remove_tst_ex=list(set(remove_tst_ex)) 138print (len(remove_tst_ex)) 139 140for i in remove_tst_ex: 141 print (i)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

patch

1-f = open('../data/FB15k_entity2idx.pkl', 'w') 2-g = open('../data/FB15k_idx2entity.pkl', 'w') 3+f = open('../data/FB15k_entity2idx.pkl', 'wb') 4+g = open('../data/FB15k_idx2entity.pkl', 'wb')

あと、cPickleは古いです。python3では素直にpickleを使ってください。

投稿2019/07/25 11:39

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jyohonoob

2019/07/26 04:55 編集

回答ありがとうございます. fとgの前についている-や+はどのような意味があるのでしょうか? また,hayataka2049さんの回答をそのまま, 以前の 78 f = open('../data/FB15k_entity2idx.pkl', 'w') 79 g = open('../data/FB15k_idx2entity.pkl', 'w') この文を -f = open('../data/FB15k_entity2idx.pkl', 'w') -g = open('../data/FB15k_idx2entity.pkl', 'w') +f = open('../data/FB15k_entity2idx.pkl', 'wb') +g = open('../data/FB15k_idx2entity.pkl', 'wb') この文に置き換えて実行してみたところ, エラー文として, File "<ipython-input-9-2acda53d9cef>", line 78 -f = open('../data/FB15k_entity2idx.pkl', 'w') ^ SyntaxError: can't assign to operator が出てきました.
hayataka2049

2019/07/26 06:18

-の行をけして同じ位置に+の行を足す
jyohonoob

2019/07/26 07:05

本当に何度も,何度もすいません. また,エラーが出てしまいました. File "FB15k_parse.py", line 84 +f = open('../data/FB15k_entity2idx.pkl', 'wb') ^ SyntaxError: can't assign to operator よろしくお願いします.
TakaiY

2019/07/26 07:56

行の先頭についている 「+」や「-」の記号は、差分の表示に使う追加/削除を示す記号ですので、取り除いてください。 f = open('../data/FB15k_entity2idx.pkl', 'wb') でOKです。
hayataka2049

2019/07/26 10:03

TakaiYさん、コメントありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問