我想从文件夹和子文件夹中读取文本文件,并用python将它们保存为一个json文件作为字典。我不确定读数是否已经正确,但在搜索时,我总是收到一条错误消息。在
这是我的索引代码#!/usr/bin/python
import sys
import glob
import os
import json
basePath = str(sys.argv[1])
allfolder = []
filename = []
fh = []
for files in glob.glob( basePath + '/*.txt' ):
filename.append(files)
for root, dirs, files in os.walk( basePath ):
allfolder.append(dirs)
searchfolder = allfolder[0]
for folder in searchfolder:
for files in glob.glob( basePath + '/' + folder + '/*.txt' ):
filename.append(files)
dic = open('index.json',"w")
info = {}
for i in filename:
fobj = open(i,"r")
for line in fobj:
zeile = line.split(" ")
for a in zeile:
b = a.strip()
if b == "":
break
dic.write(json.dumps({'wort' : b, 'pfad' : i}, indent=2))
fobj.close()
dic.close()
还有我的搜索密码
^{pr2}$
现在错误信息christoph@Notebook-CSWS:~/System/Blatt4$ python3 sucher.py a
Traceback (most recent call last):
File "sucher.py", line 15, in
data = json.load(json_data)
File "/usr/lib/python3.3/json/__init__.py", line 274, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.3/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.3/json/decoder.py", line 355, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 4 column 2 - line 54490 column 2 (char 87 - 1833872)
有谁能帮我解决我的问题吗?
提前谢谢你!在