导入CSV文件(转换为JSON格式)到Deta Base(数据库)



我尝试将CSV文件转换为JSON格式并导入到Deta Base(数据库)中。下面是我从终端运行的main.py代码。没有错误显示,但是Deta Base(数据库)是空的,甚至没有创建。有什么建议是错的,我的Python脚本?

import csv
from deta import Deta
# Initialize with a Project Key
deta = Deta("HERE-IS-DETA-ID")
# Path to CSV file
csvFilePath = r"data.csv"
# This how to connect to or create a database
db = deta.Base("simple_db_testing")

# Create and use DB
someone = deta.Base("somewhere")
# Define conversion from CSV to JSON
def csv_to_json(csvFilePath):
jsonArray = []

# Read csv file
with open(csvFilePath, encoding='utf-8') as csvf: 
# Load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf) 
print(csvReader)
# Convert each csv row into python dict
for row in csvReader: 
# Add this python dict to json array
jsonArray.append(row)
print(jsonArray)
# Inser JsonArray into DB named "someone"
for each in jsonArray:
someone.put(each)
import csv
from deta import Deta
# Initialize with a Project Key
deta = Deta("HERE-IS-DETA-ID")
# Path to CSV file
csvFilePath = r"data.csv"
# This how to connect to or create a database
db = deta.Base("simple_db_testing")

# Create and use DB
someone = deta.Base("somewhere")
# Define conversion from CSV to JSON
def csv_to_json(csvFilePath):
# Read csv file
with open(csvFilePath, encoding='utf-8') as csvf: 
# Load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
# Convert each csv row into python dict
for row in csvReader:
# Insert object into DB named "someone"
someone.put(row)

脚本末尾缺少一个调用函数。

import csv
from deta import Deta
# Initialize with a Project Key
deta = Deta("HERE-IS-DETA-ID")
# Path to CSV file
csvFilePath = r"data2.csv"
# Create and use DB - this database is being used
someone = deta.Base("somewhere2")
# Define conversion from CSV to JSON
def csv_to_json(csvFilePath):
# Read csv file
with open(csvFilePath, encoding='utf-8') as csvf: 
# Load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
# Convert each csv row into python dict
for row in csvReader:
# Insert object into DB named "someone"
someone.put(row)
# Print rows inserted into DB
print(row)
#need to call the function
csv_to_json(csvFilePath)

相关内容

  • 没有找到相关文章

最新更新