简体   繁体   English

有没有办法搜索字符串并复制前面的文本直到它到达逗号?

[英]Is there a way to search for a string and copy text in front until it reaches a comma?

I am new to python and wanted to store the recentAveragePrice inside a variable (from a string like this one)我是 python 的新手,想将recentAveragePrice 存储在一个变量中(来自像这样的字符串)

{"assetStock":null,"sales":250694,"numberRemaining":null,"recentAveragePrice":731,"originalPrice":null,"priceDataPoints":[{"value":661,"date":"2022-08-11T05:00:00Z"},{"value":592,"date":"2022-08-10T05:00:00Z"},{"value":443,"date":"2022-08-09T05:00:00Z"}],"volumeDataPoints":[{"value":155,"date":"2022-08-11T05:00:00Z"},{"value":4595,"date":"2022-08-10T05:00:00Z"},{"value":12675,"date":"2022-08-09T05:00:00Z"},{"value":22179,"date":"2022-08-08T05:00:00Z"},{"value":15181,"date":"2022-08-07T05:00:00Z"},{"value":14541,"date":"2022-08-06T05:00:00Z"},{"value":15310,"date":"2022-08-05T05:00:00Z"},{"value":14146,"date":"2022-08-04T05:00:00Z"},{"value":13083,"date":"2022-08-03T05:00:00Z"},{"value":14460,"date":"2022-08-02T05:00:00Z"},{"value":16809,"date":"2022-08-01T05:00:00Z"},{"value":17571,"date":"2022-07-31T05:00:00Z"},{"value":23907,"date":"2022-07-30T05:00:00Z"},{"value":39007,"date":"2022-07-29T05:00:00Z"},{"value":38823,"date":"2022-07-28T05:00:00Z"}]}

My current solution is this:我目前的解决方案是这样的:

var = sampleStr[78] + sampleStr[79] + sampleStr[80]

It works for the current string but if the recentAveragePrice was above 999 it would stop working and i was wondering if instead of getting a fixed number i could search for it inside the string.它适用于当前字符串,但如果 recentAveragePrice 高于 999 它将停止工作,我想知道是否可以在字符串中搜索它而不是获得一个固定数字。

Since this is json you should just be able to parse it and access recentAveragePrice :由于这是 json 你应该能够解析它并访问recentAveragePrice

import json
sample_string = '''{"assetStock":null,"sales":250694,"numberRemaining":null,"recentAveragePrice":731,"originalPrice":null,"priceDataPoints":[{"value":661,"date":"2022-08-11T05:00:00Z"},{"value":592,"date":"2022-08-10T05:00:00Z"},{"value":443,"date":"2022-08-09T05:00:00Z"}],"volumeDataPoints":[{"value":155,"date":"2022-08-11T05:00:00Z"},{"value":4595,"date":"2022-08-10T05:00:00Z"},{"value":12675,"date":"2022-08-09T05:00:00Z"},{"value":22179,"date":"2022-08-08T05:00:00Z"},{"value":15181,"date":"2022-08-07T05:00:00Z"},{"value":14541,"date":"2022-08-06T05:00:00Z"},{"value":15310,"date":"2022-08-05T05:00:00Z"},{"value":14146,"date":"2022-08-04T05:00:00Z"},{"value":13083,"date":"2022-08-03T05:00:00Z"},{"value":14460,"date":"2022-08-02T05:00:00Z"},{"value":16809,"date":"2022-08-01T05:00:00Z"},{"value":17571,"date":"2022-07-31T05:00:00Z"},{"value":23907,"date":"2022-07-30T05:00:00Z"},{"value":39007,"date":"2022-07-29T05:00:00Z"},{"value":38823,"date":"2022-07-28T05:00:00Z"}]}'''

data = json.loads(sample_string)
recent_price = data['recentAveragePrice']
print(recent_price)

outputs:输出:

731

Your replit code shows that you're acquiring JSON data from some website.您的复制代码显示您正在从某个网站获取 JSON 数据。 Here's an example based on the URL that you're using.这是一个基于您正在使用的 URL 的示例。 It shows how you check the response status, acquire the JSON data as a Python dictionary then print a value associated with a particular key.它显示了如何检查响应状态,获取 JSON 数据作为 Python 字典,然后打印与特定键关联的值。 If the key is missing, it will print None:如果缺少密钥,它将打印无:

import requests

(r := requests.get('https://economy.roblox.com/v1/assets/10159617728/resale-data')).raise_for_status()

jdata = r.json()

print(jdata.get('recentAveragePrice'))

Output: Output:

640

Your data is in a popular format called JSON (JavaScript Object Notation) .您的数据采用一种流行的格式,称为JSON(JavaScript Object 表示法) It's commonly used to exchange data between different systems like a server and a client, or a Python program and JavaScript program.它通常用于在不同系统之间交换数据,例如服务器和客户端,或 Python 程序和 JavaScript 程序。

Now Python doesn't use JSON per-se, but it has a data type called a dictionary that behaves very similarly to JSON.现在 Python 本身不使用 JSON,但它有一个称为字典的数据类型,其行为与 JSON 非常相似。 You can access elements of a dictionary as simply as:您可以简单地访问字典的元素:

print(my_dictionary["recentAveragePrice"])

Python has a built-in library meant specifically to handle JSON data, and it includes a function called loads() that can convert a string into a Python dictionary. Python has a built-in library meant specifically to handle JSON data, and it includes a function called loads() that can convert a string into a Python dictionary. We'll use that.我们将使用它。

Finally, putting all that together, here is a more robust program to help parse your string and pick out the data you need.最后,将所有这些放在一起,这是一个更强大的程序,可以帮助解析您的字符串并挑选出您需要的数据。 Dictionaries can do a lot more cool stuff, so make sure you take a look at the links above.字典可以做很多很酷的事情,所以一定要看看上面的链接。

# import the JSON library
# specifically, we import the `loads()` function, which will convert a JSON string into a Python object
from json import loads

# let's store your string in a variable
original_string = """
{"assetStock":null,"sales":250694,"numberRemaining":null,"recentAveragePrice":731,"originalPrice":null,"priceDataPoints":[{"value":661,"date":"2022-08-11T05:00:00Z"},{"value":592,"date":"2022-08-10T05:00:00Z"},{"value":443,"date":"2022-08-09T05:00:00Z"}],"volumeDataPoints":[{"value":155,"date":"2022-08-11T05:00:00Z"},{"value":4595,"date":"2022-08-10T05:00:00Z"},{"value":12675,"date":"2022-08-09T05:00:00Z"},{"value":22179,"date":"2022-08-08T05:00:00Z"},{"value":15181,"date":"2022-08-07T05:00:00Z"},{"value":14541,"date":"2022-08-06T05:00:00Z"},{"value":15310,"date":"2022-08-05T05:00:00Z"},{"value":14146,"date":"2022-08-04T05:00:00Z"},{"value":13083,"date":"2022-08-03T05:00:00Z"},{"value":14460,"date":"2022-08-02T05:00:00Z"},{"value":16809,"date":"2022-08-01T05:00:00Z"},{"value":17571,"date":"2022-07-31T05:00:00Z"},{"value":23907,"date":"2022-07-30T05:00:00Z"},{"value":39007,"date":"2022-07-29T05:00:00Z"},{"value":38823,"date":"2022-07-28T05:00:00Z"}]}
"""

# convert the string into a dictionary object
dictionary_object = loads(original_string)

# access the element you need
print(dictionary_object["recentAveragePrice"])

Output upon running this program: Output 运行此程序后:

$ python exp.py 
731

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM