我在客户端和Django服务器之间发送信息,我想使用JSON.我发送简单的信息 - 字符串列表.我试过用django.core.serializers,但是当我做的时候,我得到了
AttributeError: 'str' object has no attribute '_meta'
Run Code Online (Sandbox Code Playgroud)
看来这只能用于Django对象.如何序列化简单的Python对象?
我有一个example.csv包含内容的文件
1,"A towel,",1.0
42," it says, ",2.0
1337,is about the most ,-1
0,massively useful thing ,123
-2,an interstellar hitchhiker can have.,3
Run Code Online (Sandbox Code Playgroud)
我如何example.csv用Python 阅读?
同样,如果我有
data = [(1, "A towel,", 1.0),
(42, " it says, ", 2.0),
(1337, "is about the most ", -1),
(0, "massively useful thing ", 123),
(-2, "an interstellar hitchhiker can have.", 3)]
Run Code Online (Sandbox Code Playgroud)
如何data使用Python 写入CSV文件?
我正在尝试编写我的第一个json文件.但由于某种原因,它实际上不会写入文件.我知道它正在做某事,因为在运行转储后,我放入文件中的任何随机文本都会被删除,但它没有任何内容.毋庸置疑,但负载部分会抛出错误,因为那里什么也没有.不应该将所有json文本添加到文件中吗?
from json import dumps, load
n = [1, 2, 3]
s = ["a", "b" , "c"]
x = 0
y = 0
with open("text", "r") as file:
print(file.readlines())
with open("text", "w") as file:
dumps({'numbers':n, 'strings':s, 'x':x, 'y':y}, file, indent=4)
file.close()
with open("text") as file:
result = load(file)
file.close()
print (type(result))
print (result.keys())
print (result)
Run Code Online (Sandbox Code Playgroud) 以下是测试程序,包括中文字符:
# -*- coding: utf-8 -*-
import json
j = {"d":"?", "e":"a"}
json = json.dumps(j, encoding="utf-8")
print json
Run Code Online (Sandbox Code Playgroud)
下面是结果,看看json.dumps将utf-8转换为原始数字!
{"e": "a", "d": "\u4e2d"}
Run Code Online (Sandbox Code Playgroud)
为什么这会被打破?或者我错了什么?
我必须在这里做一些明显错误的事情.但它是什么,我该如何解决?
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> f1 = io.open('test.txt','w')
>>> f1.write('bingo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\appl\python\2.6.5\lib\io.py", line 1500, in write
s.__class__.__name__)
TypeError: can't write str to text stream
Run Code Online (Sandbox Code Playgroud)
编辑:在我的实际应用程序中,我将没有常量字符串,我将有一个常规字符串...如果unicode是问题,我如何转换为io.open需要的?
我有一个data我存储的字典:
key - 活动的ID
value- 此事件的名称,其中value是UTF-8字符串
现在,我想将此地图写入json文件.我试过这个:
with open('events_map.json', 'w') as out_file:
json.dump(data, out_file, indent = 4)
Run Code Online (Sandbox Code Playgroud)
但这给了我错误:
UnicodeDecodeError:'utf8'编解码器无法解码位置0的字节0xbf:无效的起始字节
现在,我也尝试过:
with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
out_file.write(unicode(json.dumps(data, encoding="utf-8")))
Run Code Online (Sandbox Code Playgroud)
但这会引发同样的错误:
UnicodeDecodeError:'utf8'编解码器无法解码位置0的字节0xbf:无效的起始字节
我也尝试过:
with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
out_file.write(unicode(json.dumps(data, encoding="utf-8", ensure_ascii=False)))
Run Code Online (Sandbox Code Playgroud)
但这会引发错误:
UnicodeDecodeError:'ascii'编解码器无法解码位置3114中的字节0xbf:序数不在范围内(128)
关于如何解决这个问题的任何建议?
编辑: 我相信这是导致我这个问题的路线:
> data['142']
'\xbf/ANCT25'
Run Code Online (Sandbox Code Playgroud)
编辑2:
该data变量被从文件中读取.所以,从文件中读取后:
data_file_lines = io.open(file_name, 'r', encoding='utf8').readlines()
Run Code Online (Sandbox Code Playgroud)
然后我做:
with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
json.dump(data, json_file, ensure_ascii=False)
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
TypeError:必须是unicode,而不是str
然后,我尝试使用数据字典执行此操作:
for tuple in sorted_tuples (the …Run Code Online (Sandbox Code Playgroud) 如何有效地将数据保存到文件(我将在稍后绘制)?
我从我的研究中得到了这个:
#Open new data file
f = open("data2.txt", "w")
f.write( str(yEst) ) # str() converts to string
f.close()
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的一些代码:
for i in drange2(0, 2*math.pi + 0.0634665182543392 , 0.0634665182543392):
for x in range(1,N+1):
yEst = yEst + a * cos(x* i)
#print yEst
f.write( str(yEst) ) # str() converts to string
yEst=0
f.close()
Run Code Online (Sandbox Code Playgroud)
现在,当我打开我的文件" data2.txt"时,我无法读取数据,因为它没有"有条理".我怎样才能使用下一行,f.write( str(yEst) )以便我有一个包含我的'yEst'数据的列到文件"data2.txt?感谢您提前考虑:)
PS:yEst看起来像(在data2.txt文件中):48.901347147148.605785828748.114506165947.429486 ..我希望它作为一个列: - >
48.9013471471 (new line)
48.6057858287 (new line)
48.1145061659 (new line)
47.4294863684
etc ..
Run Code Online (Sandbox Code Playgroud) 我使用 Keras 的预制模型来训练有关 URL 的数据集。结果以 .h5 格式保存,我想知道是否可以将保存的 .h5 文件转换为 JSON,以便我可以查看结果并将 JSON 文件应用到我的系统中,该文件将坏 URL 与好的 URL 分开。如果可能的话,我也想避免再次重新训练。
knownEmbeddings = []
knownNames = []
for (i, imagePath) in enumerate(imagePaths):
## SOME CODE
knownNames.append(name)
knownEmbeddings.append(vec.flatten())
data = {"embeddings": knownEmbeddings, "names": knownNames}
f = open('file.json', "wb")
f.write(json.dumps(data, indent=4))
f.close()
Run Code Online (Sandbox Code Playgroud)
看起来data像这样:
{'embeddings': [array([ 2.23568859e-04, -4.08176295e-02, -1.56606492e-02, -1.40566211e-02,
5.53448219e-04, 1.34807974e-01, 2.10583732e-02, -7.99260102e-03,
8.04360434e-02, 2.51036473e-02, -2.45967298e-03, 8.73192959e-03,
1.08047323e-02, 8.02712217e-02, 6.31465465e-02, 9.41963419e-02],
dtype=float32), array([-5.54675907e-02, 1.19409459e-02, -3.03599555e-02, -2.86714472e-02,
6.26528710e-02, 1.25348523e-01, -2.16291733e-02, -4.60545160e-02,
6.25465512e-02, -7.61162862e-02, 4.28330414e-02, 8.57844874e-02,
3.75184380e-02, -8.10878351e-02, -8.96525383e-02, 8.15552175e-02,
-9.75750014e-02, -8.24848488e-02, 9.30746570e-02, 1.71318889e-01,
1.00642473e-01, 5.39120510e-02, 1.12627009e-02, 1.40678780e-02,
-4.41719554e-02, 1.03237763e-01, 4.38372791e-02, 7.53327608e-02],
dtype=float32), …Run Code Online (Sandbox Code Playgroud)