简体   繁体   English

我在 f: 循环 python 和一个文件中的单词有问题

[英]I have a problem with for words in f: loop python and a file

I have written a simple program that goes through a list of very expensive csgo stickers and prints out the name of the sticker spotted in the API response.我编写了一个简单的程序,该程序通过一系列非常昂贵的 csgo 贴纸并打印出 API 响应中发现的贴纸名称。 I have no idea why my program only shows the name of the sticker that is last in the list我不知道为什么我的程序只显示列表中最后一个贴纸的名称

import json
import requests
import time

url = "http://steamcommunity.com/profiles/76561198231636540/inventory/json/730/2"

params = {
    'l': 'english',
    'key': '-------------------------',
    'steamid': 76561198231636540
}    

r = requests.get(url=url, params=params)

paczka_json = r.json()

paczka_des_dict = paczka_json["rgDescriptions"]

paczka_des_list = list(paczka_des_dict)

j = 0

for element in paczka_des_dict:
    linib = paczka_des_list[j]
    j += 1

with open(r"C:\Users\micha\Desktop\Visual_projects\steam_inventory\stickers.txt", "r") as f: 
    stickers = paczka_des_dict[linib]['descriptions'][6]['value']
    for words in f:
        if (words in stickers):
            print(words)

The list contains:该列表包含:

compLexity Gaming | Katowice 2014
HellRaisers | Katowice 2014
Crown (Foil) 
Clan-Mystik | Katowice 2014
Flammable (Foil)

The response I am working with:我正在处理的回应:

Sticker: Easy Peasy, Crown (Foil), Flammable (Foil), Easy Peasy

I have no idea why my code only returns Flammable (Foil) and skips Crown (Foil)我不知道为什么我的代码只返回 Flammable (Foil) 而跳过 Crown (Foil)

When I switch places Crown (Foil) with Flammable (Foil) only Crown (Foil) shows up当我将皇冠(箔)与易燃(箔)交换位置时,​​只有皇冠(箔)出现

Any ideas?有任何想法吗?


More detailed steam API response (Stickers are at the end)更详细的 Steam API 响应(贴纸在最后)

 <br><div id="sticker_info" name="sticker_info" title="Sticker" style="border: 2px solid rgb(102, 102, 102); border-radius: 6px; width=100; margin:4px; padding:8px;"><center><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/standard/lemon.a8860b6b7a01e608a7b10b559a38ee82cb197e3b.png"><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/stickers2/crown_foil.77c38fe60426ee084fd5c8fec0c680c342e05743.png"><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/community01/flammable_foil.f4741f611960bfb7d236177871bd00f7b052bdd5.png"><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/standard/lemon.a8860b6b7a01e608a7b10b559a38ee82cb197e3b.png"><br>Sticker: Easy Peasy, Crown (Foil), Flammable (Foil), Easy Peasy</center></div>

WHOLE API RESPONSE THAT MATTERS IN SOME WAY在某种程度上很重要的整个 API 响应

{
  "success": true,
  "rgInventory": {
    "19057393402": {
      "id": "19057393402",
      "classid": "3907426036",
      "instanceid": "188530139",
      "amount": "1",
      "pos": 1
    },
  },
  "rgCurrency": [],
  "rgDescriptions": {
    "3907426036_188530139": {
      "appid": "730",
      "classid": "3907426036",
      "instanceid": "188530139",
      "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJD_9W7m5a0n_L1JaKfzzoGuJJ02e2W8d6m2gztrkRoZmigItDGcgA_N1iFqwC-xr_m1J-57YOJlyVerprbwA",
      "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJD_9W7m5a0n_L1JaLummpD78A_3rmTodTwiwzkqUNoN236cteWcwFtY13RqADql7q8h5PttZzJwHdgvz5iuyif2YhKbw",
      "icon_drag_url": "",
      "name": "AWP | Asiimov",
      "market_hash_name": "AWP | Asiimov (Battle-Scarred)",
      "market_name": "AWP | Asiimov (Battle-Scarred)",
      "name_color": "D2D2D2",
      "background_color": "",
      "type": "Covert Sniper Rifle",
      "tradable": 0,
      "marketable": 1,
      "commodity": 0,
      "market_tradable_restriction": "7",
      "cache_expiration": "2020-07-19T07:00:00Z",
      "fraudwarnings": [
        "Name Tag: ''\u4f60\u662f\u6211\u7684\u5a73\u794e\u5b9d\u8d1d''"
      ],
      "descriptions": [
        {
          "type": "html",
          "value": "Exterior: Battle-Scarred"
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "High risk and high reward, the infamous AWP is recognizable by its signature report and one-shot, one-kill policy. It has been custom painted with a sci-fi design.\n\n<i>Anyone can 
predict the future... a visionary shapes it</i>"
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "The Phoenix Collection",
          "color": "9da1a9",
          "app_data": {
            "def_index": "65535",
            "is_itemset_name": 1
          }
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "<br><div id=\"sticker_info\" name=\"sticker_info\" title=\"Sticker\" style=\"border: 2px solid rgb(102, 102, 102); border-radius: 6px; width=100; margin:4px; padding:8px;\"><center><img width=64 height=48 src=\"https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/berlin2019/sig_aerial_gold.b93441810325923985b5a30cafad702beb36f4ea.png\"><img width=64 height=48 src=\"https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/cologne2015/envyus.89a88946df01bd6eb9a7e9dc02885ec043671702.png\"><br>Sticker: Aerial (Gold) | Berlin 2019, Team EnVyUs | Cologne 2015</center></div>",
          "app_data": {
            "def_index": "65535"
          }
        }
      ],
    },

I think you're having issues with the string comparison or you're not parsing enough information out我认为您在字符串比较方面遇到了问题,或者您没有解析出足够的信息

# process your HTML into a list 
sticker_line = 'Sticker: Easy Peasy, Crown (Foil), Flammable (Foil), Easy Peasy'
stickers = set(s.strip() for s in sticker_line.split(':')[1].split(',')) 

with open(sticker_file) as f:
    for line in f:
        parts = [s.strip() for s in line.rstrip().split('|')]  # separate each line 
        if parts[0] in stickers:   # compare your items
            print(parts[0]) 
sticker_response = paczka_des_dict[linib]['descriptions'][6]['value'] #to jest string napewno

with open(r"C:\Users\micha\Desktop\Visual_projects\steam_inventory\stickers.txt", "r") as f: #popraw
    for line in f:
        line2 = line.rstrip()
        if(line2 in sticker_response):
            print(line, end='')

Thanks stovfl you were right谢谢 stovfl 你是对的

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

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