私は 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()
しかし、それは書いていません。どちらが... 経験則として、望む効果が得られない場合は、どこかで間違いを犯しています。
誰か提案はありますか?