skip to Main Content

I need to insert a separator into the result, but it is not inserted

soup = BeautifulSoup(response.text,'lxml')
elements = soup.select('div.button.m')
for i in elements:
result = i.attrs['data']
print(result, sep=', ')

with code i get list of ‘data’ attributes without delimiter

{"a":{"url":"https://exapmle1.com","accountsOnly":"False"},"displayHints":"Both"} {"a":{"url":"https://exapmle2.com","accountsOnly":"False"},"displayHints":"Both"}

i get result without delimiter
I need to insert a separator into the result, but it is not inserted
like that:

{"a":{"url":"https://exapmle1.com","accountsOnly":"False"},"displayHints":"Both"}, {"a":{"url":"https://exapmle2.com","accountsOnly":"False"},"displayHints":"Both"}

if you add end=',' , then commas are inserted, but I don’t need a comma at the end (I want to import the result into json)

upd: or maybe there is an option to immediately wrap these lines in a JSON array, for example:

{"list":[{"a":{"url":"https://exapmle1.com","accountsOnly":"False"},"displayHints":"Both"}, {"a":{"url":"https://exapmle2.com","accountsOnly":"False"},"displayHints":"Both"}]}

2

Answers


  1. Chosen as BEST ANSWER

    sep wasn't working because i placed it in a for loop... only end works in for loop

    I found the answer, but now I don't know how to do my task(((

    in print (result) I get a list of json array strings, which I then need to merge into one array, from which I need to get a data table. to do this, put commas between the lines


  2. Assuming that result is a list of strings, you need to spread / splat its contents using * when you pass it to print, so it sees the values individually and can insert the separator between the values:

    > python -q
    >>> print(*['asdf', 'qwer', 'zxcv'], sep='--')
    asdf--qwer--zxcv
    

    If you just pass in the list, it will print out the list as a single item, essentially calling the __str__ method on the list object:

    >python -q
    >>> print(['asdf', 'qwer', 'zxcv'], sep='--')
    ['asdf', 'qwer', 'zxcv']
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search