[英]Import JSON Files from an entire directory into a MongoDB via a Python script
[英]Import multiple selected json files from the file directory, using Python
我在文件夾中有 23000+ json 文件。
出於測試目的,我只想讀取 10 個文件。
# Read several files
file1 = 'tweets201706221015.json'
file2 = 'tweets201706221115.json'
file3 = 'tweets201706221215.json'
file4 = 'tweets201706221315.json'
file5 = 'tweets201706221415.json'
file6 = 'tweets201706221515.json'
file7 = 'tweets201706221615.json'
file8 = 'tweets201706221715.json'
file9 = 'tweets201706221815.json'
file10 = 'tweets201706221915.json'
甚至更好 - 隨機選擇 10 個 json 文件。
我找到了這個答案,但它只是從文件夾中讀取json
文件,這對我來說不是問題。 我只想要一小部分數據。
我的代碼:
directory = some directory
files = [file1, file2, file3, file4, file5, file6, file7, file8, file9, file10]
path = directory + files
如上面評論部分所述,只需建立一個普通列表並遍歷每個文件名,例如:
#For simplicity reasons just three files - you can use a generator or read the filenames from the directory
files = ['file1.json', 'file2.json', 'file3.json']
for file in files:
f = open(file)
#Do your stuff with the file
f.close()
#Simply use a for loop to get files to name and then read each file.
for i in range (10):
filename = "tweets201706221%s15.json" % i
for line in open(filename, 'r'):
print(line)
首先使用 os.listdir() 從目錄中獲取所有 JSON 文件的列表,然后從該列表中隨機獲取 Select 10 個文件。 最后,您可以使用常開 function 循環加載這 10 個文件。
這應該有助於 select 隨機www.geeksforgeeks.org/randomly-select-n-elements-from-list-in-python/amp/
對於目錄中的隨機選擇,
import os
import random
# pick ten files from the /json/dir directory
ten_files = random.choices(os.listdir('/json/dir'), k=10)
for fi in ten_files:
with open(f'/json/dir/{fi}') as in_file:
# do something with in_file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.