SORU
6 EYLÜL 2012, PERŞEMBE


Python dosya için JSON veri yazma

JSON veri değişken data depoluyorum

Sunucudan verileri her zaman yakalamak zorunda kalmayayım, bir metin dosyasına bu yaz test için istiyorum.

Şu anda, bu çalışıyorum:

obj = open('data.txt', 'wb')
obj.write(data)
obj.close

Ve ben hata alma:

TypeError: must be string or buffer, not dict

Bunu nasıl düzeltebilirim?

CEVAP
6 EYLÜL 2012, PERŞEMBE


- data bir sözlük ve henüz JSON kodlanmış gerçek JSON kısmı unuttun. Bu şekilde yaz:

import json
with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)

Not: Çalışır hem 3.x ve 2.x

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Excel Functions

    Excel Functi

    4 NİSAN 2010
  • oHeymarvin

    oHeymarvin

    11 Temmuz 2013
  • Perihelion

    Perihelion

    23 NİSAN 2008