Split List To Diferent Cells In Google Sheets
I have cells in Google Sheets: enter image description here I need separe price, address to have: Name1 = 1run, Name2 = price, Name3 = address, Name4 = message I have this code (qu
Solution 1:
Replace " ".join(c.getText(strip=True) for c in cena)
with [c.getText(strip=True) for c in cena[:2]]
and just flatten the list.
Example:
cena = BeautifulSoup(page.content, "html.parser").select(".list-items__content__in > ul > li")
insertRow = ["site", 1234,'',[c.getText(strip=True) for c in cena[:2]] , 456654, 32452]
defflatten_list(_2d_list):
flat_list = []
for element in _2d_list:
iftype(element) islist:
for item in element:
flat_list.append(item)
else:
flat_list.append(element)
return flat_list
print('insertRow value:', insertRow)
print('Transformed Flat List:', flatten_list(insertRow))
Output:
insertRowvalue: ['site', 1234, '', ['2 890 000Kč', 'Address'], 456654, 32452]
TransformedFlatList: ['site', 1234, '', '2 890 000Kč', 'Address', 456654, 32452]
Reference:
Edit:
From what I've understood, the content of [c.getText(strip=True) for c in cena]
can have multiple sets of price and address.
You can use a for loop statement that iterates by 2 and access each set by using the iterator.
Example:
from bs4 import BeautifulSoup
import itertools
import datetime
text = """
<divclass="list-items__content list-items__content__1"><divclass="list-items__content__in"><ahref="#"class="in-heart js-heart "data-tooltip="Přidat do oblíbených"onclick="toggleFavorite(8826547, this)"><iclass="icon icon__heart-grey"></i></a></div><divclass="list-items__content__in"><h2class="list-items__item__title list-items__item__title__1"itemprop="name"><ahref="url"itemprop="url"class="js-simulate-link-target"onclick="return loadPropertyToModal(8826547);"title="some text">
some another text</a></h2><ul><li>
price1
</li><li>
Address1
</li></ul></div></div><divclass="list-items__content list-items__content__1"><divclass="list-items__content__in"><ahref="#"class="in-heart js-heart "data-tooltip="Přidat do oblíbených"onclick="toggleFavorite(8826547, this)"><iclass="icon icon__heart-grey"></i></a></div><divclass="list-items__content__in"><h2class="list-items__item__title list-items__item__title__1"itemprop="name"><ahref="url"itemprop="url"class="js-simulate-link-target"onclick="return loadPropertyToModal(8826547);"title="some text">
some another text</a></h2><ul><li>
price2
</li><li>
Address2
</li></ul></div></div>
"""
cena = BeautifulSoup(text, "html.parser").select(".list-items__content__in > ul > li")
test = [c.getText(strip=True) for c in cena]
site = 1
for i in range(0,len(test),2):
data = test[i:i+2]
insertRow = ["site", site,'', data[0], data[1] , 456654]
site = site+1
print('insertRow value:', insertRow)
Output:
insertRowvalue: ['site', 1, '', 'price1', 'Address1', 456654]insertRowvalue: ['site', 2, '', 'price2', 'Address2', 456654]
Post a Comment for "Split List To Diferent Cells In Google Sheets"