首页 > 解决方案 > JSON 文件每行打印一个字符

问题描述

我正在尝试打印主要的总称,但每次我这样做时,它每行返回一个字符,我如何制作它以便将整个总称打印在一行中。

结果:

@
g
m
a
i
l
.
c
o
m

预期结果:@gmail.com

JSON 代码:

{
    "primaryCatchall": "@gmail.com",
    "secondaryCatchall": "@gmail.com",
    "password": "password123",
    "repeat": 5
}

Python代码:

import json

with open('tempemail.txt', 'r') as myfile1:
    email1 = myfile1.read()
    with open('config.json', 'r') as config:
        PrimaryCatchall = json.load(config)
    for primaryCatchall in PrimaryCatchall['primaryCatchall']:
        with open('accounts.txt', 'a') as accounts:
            print(primaryCatchall)
            #accounts.write("\n")
            #accounts.write(email1)
            #accounts.write(primaryCatchall)

标签: pythonjsonpython-3.x

解决方案


在您的代码中,您将 JSON 作为变量 PrimaryCatchall 加载。紧接着,你为 PrimaryCatchall 运行了一个for 循环,它得到了每个单独的字符。摆脱那个for循环来解决问题


推荐阅读