Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:26:08+00:00 2026-06-14T01:26:08+00:00

def citypop(): import csv F = open(Top5000Population.txt) csvF = csv.reader(F) D = {} with

  • 0
def citypop():
  import csv                                  
  F = open("Top5000Population.txt")           
  csvF = csv.reader(F)
  D = {}
  with csvF for row in csvF:
      city,state,population = row[0],row[1],row[2] 
      population = population.replace(',','') 
      population = int(population)
      city = city.upper()[:12]
      D[(city, state)] = population
  return D

The function citypop() returns a dict with (city,state) as the key and the population of that city (in that state) as the value.

I keep getting a syntax error .. am I not understanding the csv module correctly?

EDIT: thanks for the help guys….this should work but now all of the sudden I am getting the error

 for city, state, population in reader(F):       File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/encodings/ascii.py", line 26, in decode         return codecs.ascii_decode(input, self.errors[0]) UnicodeDecodeError: 'ascii' codec can't decode byte 0xa4 in position 7062: ordinal not in range(128)  

When I run the test cases …. any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:26:09+00:00Added an answer on June 14, 2026 at 1:26 am

    Think you mean this when tried to use with statement – in such a case file will be closed right after leaving code under it:

    from csv import reader
    
    def citypop():
      D = {}
      with open("Top5000Population.txt") as F:
        for city, state, population in reader(F):
          city = city.upper()[:12]
          D[(city, state)] = int(population.replace(',',''))
      return D
    

    OR:

    def citypop():
      with open("Top5000Population.txt") as F:
        return dict(((x.upper()[:12], y), int(z.replace(',', '')) for x, y, z in reader(F))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def replace(): import tkinter.filedialog drawfilename = tkinter.filedialog.askopenfilename() list1= int(open(drawfilename,'w')) del list1[-3:] input_list = input(Enter
def read_lines(): readFileName = readfile.txt f = open(readFileName, 'r+') contents = f.read() ... #
def makecounter(): return collections.defaultdict(int) class RankedIndex(object): def __init__(self): self._inverted_index = collections.defaultdict(list) self._documents = []
def divideset(rows, column, value) split_function = nil if value.is_a?(Fixnum) || value.is_a?(Float) split_function = lambda{|row|
def initUI(self): self.columnconfigure(5, weight=1) self.rowconfigure(3, weight=1) self.search_label = Label(self, text='Keyword:') self.search_label.grid(row=0, column=0, padx=5) self.keywords
def display_home(request): from datetime import * now=datetime.today() print 'Month is %s'%now.month events=Event.objects.filter(e_date__year=datetime.today().year).filter(e_date__month=datetime.today().month,e_status=1).values('e_name','e_date') return render_to_response("SecureVirtualElection/home.html",{'events':
def comb(c: Int, r: Int): Int = { if(r == 1) c else if(r
def create_thumbnail(f, width=200, height=100): im = Image.open(f) im.thumbnail((width, height), Image.ANTIALIAS) thumbnail_file = StringIO() im.save(thumbnail_file,
def csv_parsing require 'csv' csv_file_path = File.join(File.dirname(__FILE__), csv_data.csv) CSV.parse(csv_file_path) do |line| puts line[0] end
def get_type x = [{:type=>'A', :patterns=>['foo.*']}, {:type=>'B', :patterns=>['bar.*']}] name = 'foo.txt' result = x.each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.