【问题标题】:Is there a way to search for a string and copy text in front until it reaches a comma?有没有办法搜索字符串并复制前面的文本直到它到达逗号?
【发布时间】:2022-08-11 19:19:51
【问题描述】:

我是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\"}]}

我目前的解决方案是这样的:

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

它适用于当前字符串,但如果 recentAveragePrice 高于 999 它将停止工作,我想知道是否可以在字符串中搜索它而不是获得一个固定数字。

  • sampleStr 是什么?
  • 请分享您尝试过的完整代码,否则我们只是猜测。
  • 您确定要处理字符串而不是字典吗?
  • 我在共享代码时遇到问题,但它在此复制页面中 replit.com/@ShyMike/8-Bit-Collection-Profit#main.py

标签: python python-3.x string variables search


【解决方案1】:

由于这是 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)

输出:

731

【讨论】:

    【解决方案2】:

    好吧,如果您的变量是一个字符串,您可以将其更改为 dict ,如下所示:

    import ast
    dictionary=ast.literal_eval(variable_name)
    average=dictionary['recentAveragePrice']
    

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2020-05-11
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      相关资源
      最近更新 更多