2

I can't get the slicing to work properly. I have a list of strings looking like this:

['subdomain', 'name', 'url']
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=1', 'Cümen-Cümen Empanadas Palermo', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-palermo-menu']
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=1', 'Cümen-Cümen Empanadas - Barrio Norte', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-barrio-norte-menu']

What I need is to save the 'url' in a new list to further work on it.

This is what I'm trying

for row[3:3] in reader:
   menus = []
   menus.append[row]

But this is what I get when I print():

['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=5', 'La Pergola - Recoleta', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/la-pergola-recoleta-menu']

Which is the last part of the list. What I need is:

menus = ['https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-palermo-menu', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-barrio-norte-menu']

I've added the rest of the code. The issue is that it's not a list of str as I thought but type() = '_csv.reader'

Here is the entire code:

urls = ["https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery",]

with open("output1.csv", 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(['subdomain', 'name', 'url'])

    for url in urls:
        base = url+ "?bt=RESTAURANT&page="
        page = 1
        restaurants = []

        while True:
            soup = bs(requests.get(base + str(page)).text, "html.parser")

        sections = soup.find_all("section", attrs={"class": "restaurantData"})

        if not sections: break

        for section in sections:
            for elem in section.find_all("a", href=True, attrs={"class": "arrivalName"}):
                restaurants.append({"name": elem.text, "url": elem["href"],})
                writer.writerow([base+str(page),elem.text,elem["href"]])
        page += 1    

#reading
file = open("output1.csv", 'r')
reader = csv.reader(file)
jmChrist
  • 69
  • 5

3 Answers3

1

Seems like you want this:

menus = []
for row in reader:
    menus.append(row[2])

I don't understand what you're trying to do by making row[3:3] the iterated variable of a for loop. I think you want to iterate over simple rows and then do something with each row inside the loop.

quamrana
  • 37,849
  • 12
  • 53
  • 71
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
1

Assuming you have a list of lists (ie an extra [] around your lists) and not 3 isolated lists as your question implies, you can loop through your lists of lists and take the url element from each (element 2) to append to a new list.

reader = csvreader or whatever you do to define it
menu = []    
for n, i in enumerate(reader): 
    if(n != 0):
        print(i[2])
        menu.append(i[2])

I have altered the code to work with the csvreader object. Instead of my old way to ignore the first element we will use enumerate a fantastic function that counts which element of the reader we are in as n. So as long as n is not zero we will continue like before.

Hoog
  • 2,280
  • 1
  • 14
  • 20
  • 1
    He probably wants the slice `abc[1:]`, to skip over the heading row. – Barmar May 06 '19 at 16:48
  • Good call, added – Hoog May 06 '19 at 16:50
  • I've added the entire code. Problem is type = ._csv.reader Thanks! – jmChrist May 06 '19 at 17:04
  • If I understand csv.reader correctly it should work the same as a list for my bit of code. If instead of `abc` you use: `for i in reader[1:]:` I think it should work. If not let me know the error message. – Hoog May 06 '19 at 17:09
  • Ok, that's the `[1:]` that's not allowed so the shortcut for ignoring the first line won't work this way. I'll edit the code to something that will make sense. – Hoog May 06 '19 at 17:15
0

The issue does not lie in the slicing (although you could also directly index with [2]). However, you reinitialize menus in the loop. So for every run of the loop, you overwrite what was previously there. This should fix it:

menus = []
for row in reader:
   menus.append[row[2]]

List comprehension

A cleaner (and more pythonic) approach would be to use a list comprehension:

menus = [row[2] for row in reader]
Ivo Merchiers
  • 1,589
  • 13
  • 29