Hướng dẫn convert unicode string to dictionary python - chuyển đổi chuỗi unicode sang python từ điển

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

Hướng dẫn convert unicode string to dictionary python - chuyển đổi chuỗi unicode sang python từ điển

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

Hướng dẫn convert unicode string to dictionary python - chuyển đổi chuỗi unicode sang python từ điển

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

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.
: 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.

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

Hướng dẫn convert unicode string to dictionary python - chuyển đổi chuỗi unicode sang python từ điển

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

  • Chuyển đổi chuỗi unicode thành chuỗi bằng str ()
  • Chuyển đổi chuỗi Unicode thành UTF-8
  • Chuyển đổi chuỗi Unicode thành UTF-16
  • Chuyển đổi chuỗi Unicode thành UTF-32
  • Bả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.

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)
0

Output:

from ast import literal_eval
from collections import MutableMapping

my_dict = literal_eval(my_str_dict)
assert isinstance(my_dict, MutableMapping)
1

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.

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)
3

Output:

from ast import literal_eval
from collections import MutableMapping

my_dict = literal_eval(my_str_dict)
assert isinstance(my_dict, MutableMapping)
4

Chuyể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)
5

Bả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)
6

Output:

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)
8

Example:

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)
9

Output:

import json
result = json.loads(u"{'code1':1,'code2':1}")   # will NOT work; see above
0

Chuyể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
1

Chuyể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
2

Output:

import json
result = json.loads(u"{'code1':1,'code2':1}")   # will NOT work; see above
3

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.

Syntax:

import json
result = json.loads(u"{'code1':1,'code2':1}")   # will NOT work; see above
4

Example:

Ở đâ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
5

Output:

import json
result = json.loads(u"{'code1':1,'code2':1}")   # will NOT work; see above
6

Bả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.

Làm thế nào để chuyển đổi Unicode thành một chuỗi trong Python?

Để chuyển đổi python unicode thành chuỗi, hãy sử dụng hàm unicodedata.normalize ().Tiêu chuẩn Unicode xác định các dạng chuẩn hóa khác nhau của chuỗi Unicode, dựa trên sự tương đương và tương đương tương đương chính tắc.use the unicodedata. normalize() function. The Unicode standard defines various normalization forms of a Unicode string, based on canonical equivalence and compatibility equivalence.

Python có chấp nhận Unicode không?

Loại chuỗi của Python sử dụng tiêu chuẩn Unicode để biểu diễn các ký tự, cho phép các chương trình Python hoạt động với tất cả các ký tự có thể khác nhau này., which lets Python programs work with all these different possible characters.

Làm thế nào để tôi tìm thấy unicode của một nhân vật trong Python?

Trong Python, các hàm tích hợp tích hợp chr () và ord () được sử dụng để chuyển đổi giữa các điểm và ký tự mã Unicode.Một ký tự cũng có thể được biểu diễn bằng cách viết một điểm mã unicode thập lục phân với \ x, \ u hoặc \ u theo nghĩa đen của chuỗi.the built-in functions chr() and ord() are used to convert between Unicode code points and characters. A character can also be represented by writing a hexadecimal Unicode code point with \x , \u , or \U in a string literal.

Unicode trong Python là gì?

Unicode còn được gọi là bộ ký tự phổ quát.ASCII sử dụng 8 bit (1 byte) để thể hiện một ký tự và có thể có tối đa 256 (2^8) kết hợp riêng biệt.