【问题标题】:How to assign unicode json string如何分配unicode json字符串
【发布时间】:2017-08-10 04:48:04
【问题描述】:

我正在使用 lxml 并尝试将解析的数据放入 json 字符串。但我的数据是 unicode 字符串,它会自动转换。

这是我的代码:

from lxml import html,etree
import pprint
import requests
url="http://thuvienphapluat.vn"
page = requests.get(url)
tree=html.fromstring(page.content)
vbplm=tree.xpath('//div[@id="VBPLMOI"]//div[@class="left-col"]')
rlst={}
# print etree.tostring(tree.find('./a'),pretty_print=True)
import re
for vb in vbplm:
    id = re.sub(r"\n*\s",'',vb.xpath('.//*[@class="number"]/text()')[0])
    rlst[id]={}
    tmp=vb.xpath('.//a')

    for tpm_part in tmp:
        rlst[id][
                (tpm_part.xpath('.//text()'))[0].encode(encoding='utf-8')
            ]=((tpm_part.get("href")))
        print (tpm_part.xpath('.//text()'))[0].encode(encoding='utf-8')
        print "<<<<<<<<"
        break
    break
pprint.pprint(rlst)

这是我的结果:

Văn bản hợp nhất 02/VBHN-BGDĐT năm 2017 hướng dẫn Quyết định 152/2007/QĐ-TTg về học bổng chính sách đối với học sinh, sinh viên học tại cơ sở giáo dục thuộc hệ thống giáo dục quốc dân do Bộ Giáo dục và Đào tạo ban hành
<<<<<<<<

    {'1': {'V\xc4\x83n b\xe1\xba\xa3n h\xe1\xbb\xa3p nh\xe1\xba\xa5t 02/VBHN-BGD\xc4\x90T n\xc4\x83m 2017 h\xc6\xb0\xe1\xbb\x9bng d\xe1\xba\xabn Quy\xe1\xba\xbft \xc4\x91\xe1\xbb\x8bnh 152/2007/Q\xc4\x90-TTg v\xe1\xbb\x81 h\xe1\xbb\x8dc b\xe1\xbb\x95ng ch\xc3\xadnh s\xc3\xa1ch \xc4\x91\xe1\xbb\x91i v\xe1\xbb\x9bi h\xe1\xbb\x8dc sinh, sinh vi\xc3\xaan h\xe1\xbb\x8dc t\xe1\xba\xa1i c\xc6\xa1 s\xe1\xbb\x9f gi\xc3\xa1o d\xe1\xbb\xa5c thu\xe1\xbb\x99c h\xe1\xbb\x87 th\xe1\xbb\x91ng gi\xc3\xa1o d\xe1\xbb\xa5c qu\xe1\xbb\x91c d\xc3\xa2n do B\xe1\xbb\x99 Gi\xc3\xa1o d\xe1\xbb\xa5c v\xc3\xa0 \xc4\x90\xc3\xa0o t\xe1\xba\xa1o ban h\xc3\xa0nh':
 'http://thuvienphapluat.vn/van-ban/Giao-duc/Van-ban-hop-nhat-02-VBHN-BGDDT-huong-dan-152-2007-QD-TTg-hoc-bong-chinh-sach-hoc-sinh-sinh-vien-342726.aspx'}}

它不是另存为格式“vănbảnhợpnhất02/ vbhn-bgdđtnăm2017Hướngdẫnquyếtđịnh152/ 2007 /qđ-ttgvềhọcbổngchínhsáchđốivớihīcinh,sinhviênhọctạicơsởgiáodụcthuộc hệ thống giáo dục quốc dân do Bộ Giáo dục và Đào tạo ban hành"。

请帮我创建这个 unicode json 字符串。

谢谢

【问题讨论】:

    标签: python json unicode lxml python-unicode


    【解决方案1】:

    你只有一个 Python 字典。您需要使用 json 模块来生成 JSON 字符串。

    import json
    print(json.dumps(rlst))
    

    【讨论】:

    • 可能吗?这是一个完全不同的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2015-05-09
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2021-10-16
    相关资源
    最近更新 更多