简体   繁体   English

将 CSV 转换为 jSON - 不断出现“预期文件结束”错误

[英]Converting CSV to jSON - Keep getting "End of File expected" error

I'm trying to convert a CSV file into a jSON file in order to then inject it into a Firebase database.我正在尝试将 CSV 文件转换为 jSON 文件,然后将其注入 Firebase 数据库。

csvfile = open('final_df_2.csv', 'r')
jsonfile = open('final_df_5.json', 'w')

reader = csv.DictReader(csvfile)

for row in reader:
    json.dump({row['ballID']: [{"colour": row['colour'], "radius":row['radius']}]}, jsonfile)
    jsonfile.write('\n')

Unfortunately, I keep getting an "End of File expected" error不幸的是,我不断收到“预期文件结束”错误

Here's my JSON's output这是我的 JSON output

{
    "001": [
        {
            "colour": "green",
            "radius": "199405.0"
        }
    ]
}
{
    "002": [
        {
            "colour": "blue",
            "radius": "199612.0"
        }
    ]
}

In addition, Firebase sends back an error when I try to import the JSON file saying "Invalid JSON files"此外,当我尝试导入 JSON 文件时,Firebase 发回错误消息“无效的 JSON 文件”

You could collect all the data into a python list and dump that list to the json file:您可以将所有数据收集到一个 python list ,并将该列表转储到 json 文件中:

csvfile = open('final_df_2.csv', 'r')

reader = csv.DictReader(csvfile)
jsoncontent = []
for row in reader:
    jsoncontent.append({row['ballID']: [{"colour": row['colour'], "radius":row['radius']}]})

with open('final_df_5.json', 'w') as jsonfile:
    json.dump(jsoncontent, jsonfile)

However, I'm not sure what your firebase database is expecting.但是,我不确定您的 firebase 数据库期望什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入 CSV 文件但出现时间戳错误 - Importing CSV file but getting timestamp error 在Golang中将动态JSON转换为CSV - Converting dynamic JSON to CSV in Golang 获取第 19 行:语法错误:意外的文件结尾 - Getting line 19: syntax error: unexpected end of file 语法错误:预期的输入结束但在 - Syntax error: Expected end of input but got "(" at 语法错误:应为“(”或“,”或关键字 SELECT 但脚本结束 - Syntax error: Expected "(" or "," or keyword SELECT but got end of script 语法错误:预期输入结束但在 BigQuery 中获得了标识符 - Syntax error: Expected end of input but got identifier in BigQuery Swift 预处理错误:预处理器表达式中的预期行结束 - Swift Preprocessing Error: Expected end of line in preprocessor expression 为什么我不断收到此错误:错误:Email 格式错误? - Why do I keep getting this error: Error: Email is badly formatted? 当我在 django 中使用 json 文件 firebase SDK 时出现错误 - I'm getting an error whe I use json file firebase SDK in django 出现错误:使用 Sendgrid API 的 Next.js 应用程序中的“个性化”预期数组 - Getting Error: Array expected for`personalizations` in Next js app with Sendgrid API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM