簡體   English   中英

Android如果不存在python,如何創建一個新的json文件

[英]Android How to create a new json file if it doesn't exist python

我正在創建一個應用程序,讓用戶輸入信息,並可以將數據保存到名為hello.json的json文件中。 這在文件存在時工作正常,但是當在android上測試時它崩潰了,因為它不包含這個文件。 如果它不存在,如何在android上創建一個新的json文件?

.py文件

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import AsyncImage
from kivy.uix.label import Label
from kivy.properties import StringProperty, ListProperty
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.textinput import TextInput
from kivy.network.urlrequest import UrlRequest
from kivy.storage.jsonstore import JsonStore
from os.path import exists
from kivy.compat import iteritems
from kivy.storage import AbstractStore
from json import loads, dump
from kivy.config import Config



class Phone(FloatLayout):
    def __init__(self, **kwargs):
        # make sure we aren't overriding any important functionality
        super(Phone, self).__init__(**kwargs)

    with self.canvas.before:
        Color(0, 1, 0, 1)  # green; colors range from 0-1 instead of 0-255
        self.rect = Rectangle(size=self.size, pos=self.pos)

    self.bind(size=self._update_rect, pos=self._update_rect)

    with open('hello.json') as inFile:
        try:
            data = Phone.load(self)
        except KeyError:
            data = []

def _update_rect(self, instance, value):
    self.rect.pos = instance.pos
    self.rect.size = instance.size

def product(self, instance):
    self.result.text = str(float(self.w.text) * 703/ (float(self.h.text) * float(self.h.text)))

def save(self):
    store = JsonStore('hello.json')
    name = self.n.text
    gender = self.g.text
    dtype = self.t.text
    height = self.h.text
    weight = self.w.text
    bmi = self.result.text
    medications = self.m.text
    insulin = self.ti.text
    store.put('profile', name=name, gender=gender, dtype=dtype, height=height, weight=weight, bmi=bmi, medications=medications, insulin=insulin)



def load(self):
    store = JsonStore('hello.json')
    profile = store.get('profile')
    self.n.text = profile['name']
    self.g.text = profile['gender']
    self.t.text = profile['dtype']
    self.h.text = profile['height']
    self.w.text = profile['weight']
    self.result.text = profile['bmi']
    self.m.text = profile['medications']
    self.ti.text = profile['insulin']

    try:
        store.get('profile')['name']
    except KeyError:
        name = ""
    else:
        name = store.get('profile')['name']        




presentation = Builder.load_file("main.kv")

class PhoneApp(App):
    def build(self):
        store = JsonStore('hello.json')

        return Phone()



if __name__ == '__main__':
    PhoneApp().run()

您應該使用with open('hello.json', 'a+') as inFile

a +打開文件以進行追加和閱讀。 如果文件存在,則文件指針位於文件的末尾。 該文件以追加模式打開。 如果該文件不存在,則會創建一個用於讀寫的新文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM