简体   繁体   中英

JSON file prints one character per line

I am trying to print the primary catchall but every time I do it it returns one character per line, how do I make it so it prints the whole catchall in one line.

Result:

@
g
m
a
i
l
.
c
o
m

Expected result: @gmail.com

JSON Code:

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

Python code:

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)

In your code, you loaded your JSON as the variable PrimaryCatchall. Right afterward, you ran a for loop for PrimaryCatchall, which got each individual character. Get rid of that for loop to fix the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM