没有JSON对象可以在txt

时间:2018-01-20 10:59:49

标签: json python-2.7

这是我在txt中的数据:

{ "_id" : ObjectId("5a60a394ac73c233ba1acc55"), "meta_version" : "v1.0", "download_data" : { "raw_data" : {  }, "parsed_data" : { "product_type" : "PET", "producer" : "xxx", "price" : "885/kg", "datePublished" : "2018-01-18", "fromLocation" : "", "itemCondition" : "aaa", "priceType" : "1000.0" } }, "download_config" : { "url" : "http://www.1pi.com/price/plist-798-1.html", "method" : "GET" }, "meta_updated" : "2018-01-18T21:39:32" }
{ "_id" : ObjectId("5a60a394ac73c233ba1acc56"), "meta_version" : "v1.0", "download_data" : { "raw_data" : {  }, "parsed_data" : { "product_type" : "PET", "producer" : "aaas", "price" : "8750/kg", "datePublished" : "2018-01-18", "fromLocation" : "", "itemCondition" : "AAA", "priceType" : "666.0" } }, "download_config" : { "url" : "http://www.1pi.com/price/plist-798-1.html", "method" : "GET" }, "meta_updated" : "2018-01-18T21:39:32" }

这是txt中数据的一部分,我试图用JSON解析它。这些数据不存储在txt中的行中,并且是无序的。但我在Python 2中遇到了编码问题。

def convert(filename):
    dataDict = {}
    print "Opening TXT file: ",filename
    with open(filename) as f:
        data = json.load(f)
        print(data)
if __name__ == '__main__':
    for dirname, dirnames, filenames in os.walk('../txt'):
        for filename in filenames:
            file = os.path.join(dirname, filename)
            new_file = '../txt/' + os.path.basename(file)
            print(new_file)
            convert(file)
  

文件“E:\ python2 \ lib \ json \ decoder.py”,第382行,在raw_decode中引发ValueError(“无JSON对象可被解码”)
  ValueError:无法解码JSON对象

0 个答案:

没有答案