gpt4 book ai didi

python - 从文本文件管理类似 JSON 的数据

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:25 25 4
gpt4 key购买 nike

Python新手请多多关照。我有一个 .txt 文件,其中一行包含类似 JSON 的数据:

{"marketing_package_url": "http://www.capitalpacific.com/inquiry/TrailsEndMarketplaceExecSummary.pdf", "title": "TRAILS END MARKETPLACE", "location": "OREGON CITY, OR"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Yukon-Village-YukonOK.pdf", "title": "YUKON VILLAGE", "location": "YUKON, OK"}{"marketing_package_url": "http://www.capitalpacific.com/inquiry/SouthPointPlazaExecSummary-CONFI.pdf", "title": "SOUTH POINT PLAZA", "location": "EVERETT, WA"}{"marketing_package_url": "http://www.capitalpacific.com/inquiry/HomeDepotBellinghamExecutiveSummary.pdf", "title": "HOME DEPOT - BELLINGHAM", "location": "BELLINGHAM, WA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Muncie-Marketplace-MuncieIN.pdf", "title": "MUNCIE MARKETPLACE", "location": "MUNCIE, IN"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Walmart-NeighborhoodMarket-AugustaGA.pdf", "title": "WALMART NEIGHBORHOOD MARKET", "location": "AUGUSTA, GA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Walmart-Neighborhood-Market-GainesvilleGA.pdf", "title": "WALMART NEIGHBORHOOD MARKET", "location": "GAINESVILLE, GA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Texas-Strip-Center-Portfolio.pdf", "title": "TEXAS STRIP CENTER PORTFOLIO", "location": "VARIOUS LOCATIONS, TX"}{"marketing_package_url": "http://www.capitalpacific.com/inquiry/ArneyRetailCenterExecSummary.pdf", "title": "ARNEY RETAIL CENTER", "location": "WOODBURN, OR"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Walmart-NeighborhoodMarket-LaGrangeGA.pdf", "title": "WALMART NEIGHBORHOOD MARKET", "location": "LAGRANGE, GA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Walmart-Neighborhood-Market-LynchburgVA.pdf", "title": "WALMART NEIGHBORHOOD MARKET", "location": "LYNCHBURG, VA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Walmart-Neighborhood-Market-RoanokeVA.pdf", "title": "WALMART NEIGHBORHOOD MARKET", "location": "ROANOKE, VA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Walmart-Neighborhood-Market-AshlandVA.pdf", "title": "WALMART NEIGHBORHOOD MARKET", "location": "ASHLAND, VA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Walmart-Neighborhood-Market-OklahomaCityOK.pdf", "title": "WALMART NEIGHBORHOOD MARKET", "location": "OKLAHOMA CITY, OK"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/San-Angelo-Marketplace-SanAngeloTX.pdf", "title": "SAN ANGELO MARKETPLACE", "location": "SAN ANGELO, TX"}{"marketing_package_url": "http://www.capitalpacific.com/inquiry/KeizerVillageExecSummary.pdf", "title": "KEIZER VILLAGE", "location": "KEIZER, OR"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Bonanza-Shopping-Center-ClovisCA.pdf", "title": "BONANZA SHOPPING CENTER", "location": "CLOVIS, CA"}{"marketing_package_url": "http://www.capitalpacific.com/inquiry/WalgreensBellinghamExecSummary.pdf", "title": "WALGREENS", "location": "BELLINGHAM, WA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/The-OrchardCenter-TehachapiCA.pdf", "title": "THE ORCHARD CENTER", "location": "TEHACHAPI, CA"}{"marketing_package_url": "http://cp.capitalpacific.com/Properties/Cinetopia-VancouverWA.pdf", "title": "CINETOPIA", "location": "VANCOUVER, WA"}

我想做的是仅将营销包 URLS 放到脚本中的列表中,这样它就会像这样出现:

列表[0] = http://www.capitalpacific.com/inquiry/TrailsEndMarketplaceExecSummary.pdf

列表[1] = http://cp.capitalpacific.com/Properties/Yukon-Village-YukonOK.pdf

列表[2] = ...

我已经尝试过 json.loads 但给出了错误信息,即有额外的数据或类似的东西。我认为这是因为它是一个 .txt 文件并且格式不完全像 JSON。非常感谢任何帮助。

编辑:json 对象都在一行中。这是我的第一次尝试,尝试拆分单个对象然后重新加入它们:

import json

result = []
with(open("properties.txt", "rU")) as f:
j = f.next()
jlist = len(jlist)
print len(jlist)
jlist = [jlist[0][1:] + "}"] + [ "{" + x + "}" for x in jlist[1:-1]] + ["{" + jlist[-1][:2]]
for x in jlist:
result.append(json.loads(x))

for x in result:
print(x['title'])

最佳答案

这是一个函数,它接受一个包含任意数量的 JSON 对象的字符串,这些对象相互碰撞并解析每个对象并一个一个地产生结果:

import json
def get_json_objects(s):
d = json.JSONDecoder()
idx = 0
while idx < len(s):
j, idx = d.raw_decode(s, idx=idx)
yield j

例子:

>>> list(get_json_objects("[1,2][3,4]{}"))
[[1, 2], [3, 4], {}]

所以你可以这样使用它:

urls = [j["marketing_package_url"] for j in get_json_objects(open("data.txt").read())]

关于python - 从文本文件管理类似 JSON 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com