54
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi có Unicode
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
7 và tôi muốn nó ở định dạng từ điển.Tôi muốn nó ở định dạng
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
8.Tôi đã thử
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
9 nhưng nó trả về chuỗi không phải từ điển.Ai giúp tôi với?
hỏi ngày 19 tháng 2 năm 2013 lúc 5:10Feb 19, 2013 at 5:10
Bạn có thể sử dụng gói
import ast
job1 = {}
with open['hostdata2.json'] as f:
job1= json.loads[f.read[]]
f.close[]
#print type before converting this from unicode to dic would be
print type[job1]
job1 = ast.literal_eval[job1]
print "printing type after ast"
print type[job1]
# this should result
for each in job1:
print each
print "printing keys"
print job1.keys[]
print "printing values"
print job1.values[]
0 tích hợp:import ast
d = ast.literal_eval["{'code1':1,'code2':1}"]
Trợ giúp về chức năng literal_eval trong mô -đun AST:
literal_eval[node_or_string]
Đánh giá an toàn một nút biểu thức hoặc một chuỗi chứa biểu thức python. Chuỗi hoặc nút được cung cấp chỉ có thể bao gồm các cấu trúc theo nghĩa đen Python sau: chuỗi, số, bộ dữ liệu, danh sách, dicts, booleans, và không có.
Đã trả lời ngày 19 tháng 2 năm 2013 lúc 5:19Feb 19, 2013 at 5:19
Agaagaaga
Phim thương hiệu vàng 27.4K1111 gold badges82 silver badges118 bronze badges
5
Bạn có thể sử dụng
import ast
job1 = {}
with open['hostdata2.json'] as f:
job1= json.loads[f.read[]]
f.close[]
#print type before converting this from unicode to dic would be
print type[job1]
job1 = ast.literal_eval[job1]
print "printing type after ast"
print type[job1]
# this should result
for each in job1:
print each
print "printing keys"
print job1.keys[]
print "printing values"
print job1.values[]
1. Bạn cũng có thể muốn chắc chắn rằng bạn đang tạo ra một dict và không phải là thứ khác. Thay vì import ast
job1 = {}
with open['hostdata2.json'] as f:
job1= json.loads[f.read[]]
f.close[]
#print type before converting this from unicode to dic would be
print type[job1]
job1 = ast.literal_eval[job1]
print "printing type after ast"
print type[job1]
# this should result
for each in job1:
print each
print "printing keys"
print job1.keys[]
print "printing values"
print job1.values[]
2, hãy sử dụng xử lý lỗi của riêng bạn.from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
Đã trả lời ngày 19 tháng 2 năm 2013 lúc 5:19Feb 19, 2013 at 5:19
Agaagapyrospade
Phim thương hiệu vàng 27.4K113 gold badges34 silver badges51 bronze badges
Bạn có thể sử dụng
1. Bạn cũng có thể muốn chắc chắn rằng bạn đang tạo ra một dict và không phải là thứ khác. Thay vì import ast
job1 = {}
with open['hostdata2.json'] as f:
job1= json.loads[f.read[]]
f.close[]
#print type before converting this from unicode to dic would be
print type[job1]
job1 = ast.literal_eval[job1]
print "printing type after ast"
print type[job1]
# this should result
for each in job1:
print each
print "printing keys"
print job1.keys[]
print "printing values"
print job1.values[]
2, hãy sử dụng xử lý lỗi của riêng bạn.: Turns out my assumption was incorrect; because the keys are not wrapped in double-quote marks ["], the string isn't JSON. See here for some ways around this.import ast
job1 = {}
with open['hostdata2.json'] as f:
job1= json.loads[f.read[]]
f.close[]
#print type before converting this from unicode to dic would be
print type[job1]
job1 = ast.literal_eval[job1]
print "printing type after ast"
print type[job1]
# this should result
for each in job1:
print each
print "printing keys"
print job1.keys[]
print "printing values"
print job1.values[]
pyrospadepyrospade
7.5923 Huy hiệu vàng34 Huy hiệu bạc51 Huy hiệu Đồng
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
Chỉnh sửa: Hóa ra giả định của tôi không chính xác; Bởi vì các phím không được bọc trong các điểm trích dẫn kép ["], chuỗi không phải là JSON. Xem ở đây để biết một số cách xung quanh điều này.Apr 8, 2014 at 9:31
1
Tôi đoán rằng những gì bạn có có thể là JSON, a.k.a. ký hiệu đối tượng JavaScript.
import ast
job1 = {}
with open['hostdata2.json'] as f:
job1= json.loads[f.read[]]
f.close[]
#print type before converting this from unicode to dic would be
print type[job1]
job1 = ast.literal_eval[job1]
print "printing type after ast"
print type[job1]
# this should result
for each in job1:
print each
print "printing keys"
print job1.keys[]
print "printing values"
print job1.values[]
Bạn có thể sử dụng mô-đun
import ast
job1 = {}
with open['hostdata2.json'] as f:
job1= json.loads[f.read[]]
f.close[]
#print type before converting this from unicode to dic would be
print type[job1]
job1 = ast.literal_eval[job1]
print "printing type after ast"
print type[job1]
# this should result
for each in job1:
print each
print "printing keys"
print job1.keys[]
print "printing values"
print job1.values[]
3 tích hợp của Python để thực hiện việc này:Jan 29, 2018 at 22:57
1
Đã trả lời ngày 8 tháng 4 năm 2014 lúc 9:31
>>> string_dict = u"{'code1':1, 'code2':1}"
>>> eval[string_dict]
{'code1': 1, 'code2': 1}
Đã trả lời ngày 19 tháng 2 năm 2013 lúc 5:19Feb 19, 2013 at 5:19
AgaagaAli-Akber Saifee
Phim thương hiệu vàng 27.4K111 gold badge16 silver badges18 bronze badges
3
Trong hướng dẫn Python này, bạn sẽ tìm hiểu cách chuyển đổi chuỗi Unicode thành chuỗi. Mục lục
Một chuỗi unicode được sử dụng để thể hiện các ký tự trong một hệ thống số. Nếu chúng ta muốn chỉ định một chuỗi unicode, chúng ta phải đặt ký tự - thì U U ở phía trước chuỗi.
Example:
u"Hello Varun"
Ở đây, chúng tôi sẽ sử dụng str [] để chuyển đổi chuỗi unicode thành chuỗi.
Quảng cáo
Syntax:
str[inp_str]
Chỉ cần một tham số.
Parameter:
Trong đó inp_str là chuỗi unicode.example 1:
Example 1:
Trong ví dụ này, chúng tôi sẽ chuyển đổi chuỗi unicode - u Welcome welcome to thispulum thành một chuỗi bằng cách sử dụng str [].
# Consider the unicode string inp_str= u"Welcome to thisPointer" # Convert to string print["Converted String: ",str[inp_str]]
Output:
Converted String: Welcome to thisPointer
Chuyển đổi chuỗi Unicode thành UTF-8
Chuyển đổi chuỗi Unicode thành UTF-16
Syntax:
inp_str.encode['UTF-8']
Chuyển đổi chuỗi Unicode thành UTF-32
Example:
Bản tóm tắt
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
0Output:
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
1Một chuỗi unicode được sử dụng để thể hiện các ký tự trong một hệ thống số. Nếu chúng ta muốn chỉ định một chuỗi unicode, chúng ta phải đặt ký tự - thì U U ở phía trước chuỗi.
Syntax:
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
2Ở đây, chúng tôi sẽ sử dụng str [] để chuyển đổi chuỗi unicode thành chuỗi.
In this example, we will convert the Unicode string – u”Welcome to thisPointer” to UTF-8 and again decode it to a unicode string.
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
3Output:
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
4Chuyển đổi chuỗi Unicode thành UTF-16
Chuyển đổi chuỗi Unicode thành UTF-32
Syntax:
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
5Bản tóm tắt
Example:
Một chuỗi unicode được sử dụng để thể hiện các ký tự trong một hệ thống số. Nếu chúng ta muốn chỉ định một chuỗi unicode, chúng ta phải đặt ký tự - thì U U ở phía trước chuỗi.
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
6Output:
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
7Ở đây, chúng tôi sẽ sử dụng str [] để chuyển đổi chuỗi unicode thành chuỗi.
Syntax:
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
8Example:
Quảng cáo
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval[my_str_dict]
assert isinstance[my_dict, MutableMapping]
9Output:
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
0Chuyển đổi chuỗi Unicode thành UTF-32
Bản tóm tắt
Syntax:
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
1Chuyển đổi chuỗi Unicode thành UTF-32
Example:
Bản tóm tắt
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
2Output:
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
3Một chuỗi unicode được sử dụng để thể hiện các ký tự trong một hệ thống số. Nếu chúng ta muốn chỉ định một chuỗi unicode, chúng ta phải đặt ký tự - thì U U ở phía trước chuỗi.
Syntax:
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
4Example:
Ở đây, chúng tôi sẽ sử dụng str [] để chuyển đổi chuỗi unicode thành chuỗi.
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
5Output:
import json
result = json.loads[u"{'code1':1,'code2':1}"] # will NOT work; see above
6Bản tóm tắt
Một chuỗi unicode được sử dụng để thể hiện các ký tự trong một hệ thống số. Nếu chúng ta muốn chỉ định một chuỗi unicode, chúng ta phải đặt ký tự - thì U U ở phía trước chuỗi.