python字典导入mongodb_Python:将整个JSON目录转换为Python字典以发送到MongoDB

我对Python相对来说比较陌生,对MongoDB也是一个新手,因此我只关心获取文本文件并转换它们。我目前正在尝试使用JSON中的一堆.txt文件将它们移动到MongoDB中。因此,我的方法是打开目录中的每个文件,读取每行,将其从JSON转换为字典,然后将该行作为JSON作为字典进行重写。然后它将以一种格式发送到MongoDB

(如果我的推理有任何缺陷,请指出)

目前,我写过这样的内容:

"""

Kalil's step by step iteration / write.

JSON dumps takes a python object and serializes it to JSON.

Loads takes a JSON string and turns it into a python dictionary.

So we return json.loads so that we can take that JSON string from the tweet and save it as a dictionary for Pymongo

"""

import os

import json

import pymongo

rootdir='~/Tweets'

def convert(line):

line = file.readline()

d = json.loads(lines)

return d

for subdir, dirs, files in os.walk(rootdir):

for file in files:

f=open(file, 'r')

lines = f.readlines()

f.close()

f=open(file, 'w')

for line in lines:

newline = convert(line)

f.write(newline)

f.close()但它不是写作。

哪个......作为一个经验法则,如果你没有得到你想要的效果,那么你在某个地方犯了一个错误。

有没有人有什么建议?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值