Ich versuche, einen Schreibvorgang von meinem Skript zu machen, um es zu einer JSON-Datei.Python - Json schreiben, will nicht ausdrucken, wie ich es wünsche
Allerdings, wenn ich mit meinem Skript schreiben, es gibt mir eine Ausgabe von
{
"profile_1": {
"email": "HelloWorld",
"pswrd": "WorldHello123"
}
} {
"profile_2": {
"email": "HelloWorld",
"pswrd": "WorldHello123"
}
} {
"profile_3": {
"email": "HelloWorld",
"pswrd": "WorldHello123"
}
}
Welche denn ich wirklich wollte:
"profile_0": {
"email": "HelloWorld",
"pswrd": "WorldHello123"
},
"profile_1": {
"email": "HelloWorld",
"pswrd": "WorldHello123""
},
"profile_2": {
"email": "HelloWorld",
"pswrd": "WorldHello123"
},
"profile_3": {
"email": "HelloWorld",
"pswrd": "WorldHello123"
},
und wie wir vergleichen es ist {} zu viele und auch fehlt, was ich nicht ganz verstehen kann, warum es nicht funktionieren würde. Dies ist, wie mein Skript funktioniert:
import json
with open("test.txt") as accounts:
for splitlines in accounts:
temp = accounts.read().splitlines()
username, password = zip(*(s.split(":") for s in temp))
linelength = len(username)
with open("profiles.json", "a") as profile:
lineInt = 0
while (lineInt < linelength):
lineInt += 1
jsonData = {
"profile_" + str(lineInt): {
"email": username[lineInt],
"pswrd": password[lineInt]
}
}
json.dump(jsonData, profile)
Ich habe zu lange gesessen nicht das Problem sehen: '(
Sie nur ** eine ** 'json.dump' haben sollte, bauen eine Liste von dictionari es, dann schreibe * das * in die Datei. – jonrsharpe