-1
import re
import requests
t=[]
u = []
n = []
k = []
exclude_banned = True
with open("u.txt", "r") as f:
    h = f.readlines()[0:100]
    for sub in h:
        t.append(re.sub('\n', '', sub))
    print(type(t))
    print(t)
    r = requests.post('https://users.roblox.com/v1/usernames/users', json={'usernames': t, 'excludeBannedUsers': exclude_banned})
    print(r.json())
    y = r.json()['data']
    u.extend([f['id'] for f in y])
    n.extend([f['name'] for f in y])
    k.append(f'{u[0]}:{n[0]}\n{u[1]}:{n[1]}')
    print(k)

I want to join all the variables in list u to list n like how they are joined in list k. How would I do this on a massive scale?

I tried the method in list k, but that would be very time-consuming and now all the lists are always the same lengths.

The text in the file is this: https://gist.githubusercontent.com/deekayen/4148741/raw/98d35708fa344717d8eee15d11987de6c8e26d7d/1-1000.txt

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Hi sorry I am very bad at python you'd have to show me a working example for me to understand what the .extend method is thank you! – Notepad noobb Feb 20 '23 at 14:42
  • Does this answer your question? [How do I concatenate two lists in Python?](https://stackoverflow.com/questions/1720421/how-do-i-concatenate-two-lists-in-python) – Kulasangar Feb 20 '23 at 14:44
  • It would be helpful if you shared sample from `u.txt`, the json/object in `y`, and desired results. – JNevill Feb 20 '23 at 14:45
  • Hi Jnevill I did add examples people removed my link it was annoying. – Notepad noobb Feb 20 '23 at 14:51
  • 1
    The question doesn't make it clear, but it looks like you want zip https://docs.python.org/3/library/functions.html#zip instead of (or rather in addtion to?) a simple +/extend. Although I'd recommend a dataclass with name and id, and make that f-string a method. – Kenny Ostrom Feb 20 '23 at 14:51
  • Hi Kenny my question was seemly clear for it has been answered below. – Notepad noobb Feb 20 '23 at 14:55

2 Answers2

0

I would suggest a couple of simplifications. If all you are doing with re is stripping off the trailing newlines there is a better way for example. After that, I think you really want to look at a list comprehension to build your final user data.

Check out:

import requests

## ---------------------------
## Read usernames out of a text file.
## ---------------------------
with open("u.txt", "r") as file_in:
    post_data = {
        "usernames": [username.strip() for username in file_in][:100],
        "excludeBannedUsers": True
    }
## ---------------------------

post_url = "https://users.roblox.com/v1/usernames/users"
api_response = requests.post(post_url, post_data)

if not api_response.ok():
    print("something went wrong")
    exit()

## ---------------------------
## Build a list of dictionaries based on the api data
## ---------------------------
user_data = [
    {user["id"]: user["name"]}
    for user
    in api_response.json()["data"]
]
## ---------------------------

## ---------------------------
## Print the results
## ---------------------------
import json
for user in user_data:
    print(json.dumps(user, indent=4))
## ---------------------------

Addendum:

To work through the list in batches of 100 ignoring errors you might do:

import requests

post_url = "https://users.roblox.com/v1/usernames/users"

## ---------------------------
## Read usernames out of a text file.
## ---------------------------
with open("u.txt", "r") as file_in:
    all_username = [username.strip() for username in file_in]
## ---------------------------

## ---------------------------
## process the usernames in batches of n=100
## ---------------------------
batch_size = 100
user_data = []
for i in range(0, len(all_username), batch_size):
    post_data = {
        "usernames": all_username[i: i + batch_size],
        "excludeBannedUsers": True
    }

    try:
        api_response = requests.post(post_url, post_data)
    except requests.HTTPError as e:
        print(e)
        continue

    ## ---------------------------
    ## add users to list
    ## ---------------------------
    user_data.extend([
        {user["id"]: user["name"]}
        for user
        in api_response.json()["data"]
    ])
    ## ---------------------------

    
## ---------------------------
## Print the results
## ---------------------------
import json
for user in user_data:
    print(json.dumps(user, indent=4))
## ---------------------------
JonSG
  • 10,542
  • 2
  • 25
  • 36
-1
import re
import requests

t=[]
u = []
n = []
k = []
exclude_banned = True

with open("u.txt", "r") as f:
    h = f.readlines()[0:100]
    for sub in h:
        t.append(re.sub('\n', '', sub))
    print(type(t))
    print(t)
    r = requests.post('https://users.roblox.com/v1/usernames/users', json={'usernames': t, 'excludeBannedUsers': exclude_banned})
    print(r.json())
    y = r.json()['data']
    u.extend([f['id'] for f in y])
    n.extend([f['name'] for f in y])

    # Concatenate elements in u and n using a loop
    for i in range(len(u)):
        k.append(f'{u[i]}:{n[i]}')

    print(k)