#๐Ÿ”’ I am not getting expected data

28 messages ยท Page 1 of 1 (latest)

crisp sundial
#

I am getting this data


1111How many cities do you want to see?1
Enter the city name: newyork
<_csv.reader object at 0x75baf31af0> has <_csv.reader object at 0x75baf31af0> people
Enter the city name:


Idk what the data is

pure jewelBOT
#

@crisp sundial

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

crisp sundial
#


import pandas as pd
import csv

with open('file.csv', 'r') as file:
    reader = csv.reader(file)
    
    num_cities = int(input('How many cities do you want to see? '))
    
    for _ in range(num_cities):
        city_name = input('Enter the city name: ')
        
        for row in reader:
            city, population = row.split(',')
            if city == city_name:
                   print(f'{city} has {population} people')
                   break
    file.close()


eternal otter
#

It looks like you're printing the reader object instead of specific data from it

eternal otter
#

for starters, you don't need file.close() when using with

eternal otter
#

you also can't iterate through a csv file more than once without reopening it

#

they are "exhaustable"

eternal otter
#

you should be able to convert the reader data to a list first

crisp sundial
#

Eh I got It that I had been not completed the video on CSV yet

crisp sundial
#

List(reader)

#

โ€ฆ?

eternal otter
#

reader = list(csv.reader(file))

crisp sundial
#

Ok

#

And then

#

!clise

#

!clase

pure jewelBOT
#
Did you mean...

ยป classmethod
ยป class

crisp sundial
#

!clos

crisp sundial
#

!close

pure jewelBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.