user14196173
user14196173

Reputation:

How can I split the useful information from this string and output it separately?

I am using the steam api with python and returning the recently played games of a user. It returns this (albeit as long as how many games the user has launched in the past two weeks, so I will need to limit the amount that is in the string too). [<SteamApp "Blender" (365670)>, <SteamApp "fpsVR" (908520)>, <SteamApp "OVR Toolkit" (1068820)>, <SteamApp "SteamVR" (250820)>]

My code is the following:

    async def recently_played(self, ctx, userurl):
        steam_user = steamapi.user.SteamUser(userurl=userurl)
        await ctx.send(steam_user.recently_played)

I tried to remove speech marks, square brackets, arrows and commas but I thought... what if the game has these in their names? I also could not figure out how to get inbetween each parentheses to get rid of the SteamAppID code.

Upvotes: 0

Views: 68

Answers (2)

SpencerLS
SpencerLS

Reputation: 371

If you are just trying to get an easy-to-read list of the games you could do something like this if the data you are receiving is a string:

# get this string from steam_user.recently_played
unformatted = '[<SteamApp "Blender" (365670)>, <SteamApp "fpsVR" (908520)>, <SteamApp "OVR Toolkit" (1068820)>, <SteamApp "SteamVR" (250820)>]'

gameList = unformatted.split('"')[1::2] # returns a list of the games. This is also possible with a regular expression
games = ", ".join(gameList)

print(games)
# returns Blender, fpsVR, OVR Toolkit, SteamVR

Upvotes: 0

Paul M.
Paul M.

Reputation: 10809

What you have there isn't a string or list of strings, it's a list of SteamApp objects. Simply access each SteamApp object's name property to construct a list of names, or to simply print them. There is no need to do all this string manipulation:

names = [app.name for app in user.recently_played] # a list of strings

Or:

for app in user.recently_played:
    print(app.name)

Take a look at the source code of SteamUser.recently_played and SteamApp.name.

Upvotes: 1

Related Questions