jsonファイルからtags部分のみを抽出して新しいファイルに書き出すという作業をしています。
環境はMacでpython2.7(anaconda経由)を使用しております。
ソースコードは以下の通りです。
python
1import codecs 2import json 3import glob 4 5 6allfile = glob.glob("*.jsonl") 7 8for file in allfile: 9 data = [] 10 with codecs.open(file,"r","utf-8") as f: 11 for i in f: 12 data.append(json.loads(i)) 13 14 text = [] 15 for i in data: 16 text.extend(i["tags"]) 17 18 with codecs.open("new_tags.txt","a","utf-8") as nt: 19 for txt in text: 20 nt.writelines(txt+"\n")
始めはうまく抽出処理ができていたのですが、途中のファイルから
ValueError: Unterminated string starting at: line 1 column 131 (char 130)
というエラーが出ており、処理を進められません。
扱っているjsonファイルの中身は以下のようなものです。(先頭1行)
jsonl
1{"video_id":"nm16580085","watch_num":81,"comment_num":2,"mylist_num":0,"title":"ほとんど紅魔郷風","description":"初投です。まだまだ未熟ですが・・・・よろしくおねがいします","category":"東方","tags":["東方"],"upload_time":1325364768,"file_type":"swf","length":281,"size_high":3125649,"size_low":3125596}
調べても原因がよくわからない初心者です。
###Traceback (発生しているメッセージ)
runfile('/Users/yo/Downloads/json_l.py', wdir='/Users/yo/Downloads') Traceback (most recent call last): File "<ipython-input-7-41ae6a345b74>", line 1, in <module> runfile('/Users/yo/Downloads/json_l.py', wdir='/Users/yo/Downloads') File "/Users/yo/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile execfile(filename, namespace) File "/Users/yo/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile builtins.execfile(filename, *where) File "/Users/yo/Downloads/json_l.py", line 19, in <module> data.append(json.loads(i)) File "/Users/yo/anaconda/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/Users/yo/anaconda/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/Users/yo/anaconda/lib/python2.7/json/decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Unterminated string starting at: line 1 column 131 (char 130)
どなたか同じ経験がある方がおりましたら、どういう状況なのか、改善案のヒントとなることがあれば教えていただけませんでしょうか?
回答1件
あなたの回答
tips
プレビュー