Hallo, und danke noch einmal an BlackJack für die sehr offene Antwort
BlackJack hat geschrieben:Die API ist eine Katastrophe
Es gibt noch zwei dinge die ich generell anmerken muss, da ich sie wahrscheinlich nicht ausreichend ausgedrückt habe:
Der Code ist als unterprogramm gemeint um ein größeres script mit einer datei interfacen zu lassen. Deswegen würde es keine multiline jsons geben, und der code sieht nutzlos aus für eine anwendung durch menschen.
Und ich bin noch relativ neu zu PYTHON, Ich habe schon länger programmiert, kenne aber noch nicht alle möglichkeiten und module, die Python bietet, deswegen schreibe ich manchmal viel code, das dinge macht, die ein simpler import lösen könnte. Es ist großartig, dass ihr mich auf dies module hinweißt.
BlackJack hat geschrieben:die ganzen Dateitests sind Mist
was meinst du mit Dateitests? die ganzen if-statements
BlackJack hat geschrieben:JSON kann auch mehr als eine Zeile enthalten, auch wenn *Du* das jetzt nicht so schreibst (an welcher Stelle ist das eigentlich garantiert?)
der code ist nur ale kleiner teil eines grösseren Programms gemeint. wenn man nur den code benutzt um mit diner datei zu interfacen, dann gibt es keine multiline statements.
BlackJack hat geschrieben:und natürlich nutzt Du ``+`` um Zeichenketten zusammenzusetzen.
da habe ich mich vertippt.

und ich bin jetzt auch schon davon weg.
Sirius, meinst du so etwas mit dem abfangen?
Code: Alles auswählen
import os
import json
"""
most of the def's are created for a list with following format:
{"titleOne":valueOne,"titleTwo":valueTwo,"titleThree":valueThree}
however:
-createFile
-read
-write
should work for any json formatted data
createFile(path, filename) creates a file with name FILENAME at PATH
read(filePath) returns the read out text from file PATH must be valid json
write(filePath, data) writes DATA in the file PATH must be valid json
titleList(data) returns a list with the titles in DATA. format: see above
indexInList(data, title) returns the index of TITLE in DATA. format: see above
getValue(data, title) returns the value of TITLE in DATA. format: see above
removeFromList(data, title) removes TITLE from DATA. format: see above
inList(data, title) returns True if TITLE is in DATA. format: see above
setValue(data, title, value) sets TITLE in DATA to VALUE. format: see above
addToList(data, title, value) adds TITLE with value VALUE to DATA. format: see above
rename(data, title, newTitle) renames TITLE in DATA to NEWTITLE. format: see above
created by kamik423
"""
def createFile(path, filename):
filepath=os.path.join(path, filename)
try:
try:
os.makedirs(path)
with open(filepath, 'w') as VDfile:
VDfile.write("{}")
def read(filePath):
try:
with open(filePath, 'r') as VDfile:
try:
fileData=json.loads(VDfile.readline())
return fileData
except:
return []
except:
return []
def write(filePath, data):
try:
with open(filePath, 'w') as VDfile:
try:
VDfile.write(json.dumps(data))
def titleList(data):
titles = []
try:
dataItems = data.items()
for i in dataItems:
titles.append(i[0])
return titles
except:
return []
def indexInList(data, title):
listOfTitles = titleList(data)
try:
return listOfTitles.index(title)
else:
return None
def getValue(data, title):
titleIndex = indexInList(data, title)
try:
value = data.items()[titleIndex][1]
return value
else:
return -1
def removeFromList(data, title):
try:
titleIndex = indexInList(data, title)
value = getValue(data,title)
dictValue = {title:value}
data.pop(title)
return data
def inList(data, title):
return title in titleList(data)
def setValue(data, title, value):
try:
print data[title]
data[title]=value
return data
def addToList(data, title, value):
data[title]=value
return data
def rename(data, title, newTitle):
if inList(data, title):
value = getValue(data,title)
data = removeFromList(data,title)
data = addToList(data, str(newTitle), value)
return data