P
P
PreFireSkills2020-05-19 03:44:08
Python
PreFireSkills, 2020-05-19 03:44:08

How to read a list in a file with a given segment?

Goodnight.
I take the product id from the file and insert it into the request url. The problem is that there are too many product id in the file (more than 100000), how can I read the file to insert the first 400 id into the request, then the next 400 and so on until the end file?

def get_product_data():

    payload = {
        'product_ids': ''.join(
            [line.strip() for line in open('id.json', 'r')]
        )
    }
    f = requests.get('https://example.com./v2/product/getDetails?', params=payload)
    d = f.json()
    return d

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-05-19
@PreFireSkills

>>> ids = [x for x in range(10)]
>>> ids
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> ids = [ids[n:n+3] for n in range(0, len(ids), 3)]
>>> ids

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question