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)
4
  • 3
    Possible duplicate of Understanding slice notation Commented May 6, 2019 at 16:42
  • 2
    If you want only the url, then you don't want a slice at all; you just want a single item. Use row[2]. Commented May 6, 2019 at 16:42
  • It should be for row in reader: Commented May 6, 2019 at 16:46
  • @chepner apparently my brain isn;t working today. You are correct, of course. Commented May 6, 2019 at 16:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

He probably wants the slice abc[1:], to skip over the heading row.
Good call, added
I've added the entire code. Problem is type = ._csv.reader Thanks!
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.
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.
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.

Comments

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]

4 Comments

I get: NameError: name 'row' is not defined for your first code snippet.
@quamrana Ah, didn't actually run the code. This should have fixed it.
Hmm, row[3:3] would be empty given the data from the OP.
That's the issue I was getting and going crazy over! hahah. Problem is because it's not a list of str but a '_csv.reader' type. I've added the entire code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.