###前提・実現したいこと
jsonデータ内のtagsの要素数を新しいファイルに書き込む作業をしています。
コードは通るのですが、新しいファイルに要素数が書き込まれません。
以下のようなエラーが出ております。
解決法がわかりません、どなたかご教示頂けませんでしょうか。
###発生している問題・エラーメッセージ
File "~/python2.7/codecs.py", line 377, in writelines self.write(''.join(list)) TypeError: can only join an iterable
###該当のソースコード
python
1import codecs 2import json 3import glob 4 5 6allfile = glob.glob("*.jsonl") 7 8for file in allfile: 9 data = [] 10 k = 0 11 print(file) 12 with codecs.open(file,"r","utf-8") as f: 13 for i in f: 14 try: 15 data.append(json.loads(i)) 16 except ValueError: 17 print(i) 18 k += 1 19 20 text = [] 21for datum in data: 22 text.append(len(datum["tags"])) 23 24 with codecs.open("number_of_tags.txt","a","utf-8") as nt: 25 for txt in text: 26 nt.writelines(txt)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/15 18:18