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 8093209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:27:16+00:00 2026-06-05T20:27:16+00:00

I am having some difficulty converting this list of strings into a list of

  • 0

I am having some difficulty converting this list of strings into a list of floats. I tried this two ways and each returned different errors.

import csv
import math

unemp_reader = csv.reader(open('unemp.csv', 'rU'))
unemp_lines = list(unemp_reader)

for rows in unemp_lines:     #tried this way, but error tells me indices must be integers 
    i = 1
    for i in rows:
        a = map(float, unemp_lines[i])
        float_list.append(a)
    print float_list

for row in unemp_lines:  #tried this way but the list returned is empty
    y = row[1].split(",")[1:-1]
    float_list = [float(i) for i in y if i]
print float_list
  • 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-05T20:27:16+00:00Added an answer on June 5, 2026 at 8:27 pm

    Your issue here in your first example is that a for loop doesn’t give indices, it gives the values from the list. This means that your example doesn’t really make any sense at all.

    The second example takes the second item in the row, splits it on commas (which should all be dealt with by the csv module anyway) and then takes the second through second to last item of the resulting list. As I imagine there were no commas in the value this will take [1:-1] of a list with one element, returning an empty list. I can’t really understand the intent here. You also then only store the data from the last row (overwriting float_list each time). You appear to be second-guessing the csv module and making this a lot harder than it is.

    You need to stop overcomplicating it:

    with open('unemp.csv', 'rb') as data:
        rows = csv.reader(data)
        next(rows) #Skip the headers.
        floats = [[float(item) for number, item in enumerate(row) if item and (1 <= number <= 12)] for row in rows]
    
    print(floats)
    

    To explain, first we use the with statement to open the file in a readable and pythonic way (which ensures the file is closed properly, even on exceptions). We then make a csv.reader to get our data from the CSV file. We skip the headers by advancing the iterator by one, meaning we start on the second row. We then use a list comprehension to produce a new list from the iterator, containing another list comprehension generating the floats of the values if those values exist, and are not in the Year or Annual column. To do this we use the enumerate() builtin to get the number of the column we are in, then do a check to ensure it’s not 0 (Year) or 13 (Annual).

    As J.F.Sebastian points out in the comments, the best solution is to allow the csv module to handle dealing with the numbers for you, by adding the named argument quoting to the csv.reader() call with the value csv.QUOTE_NONNUMERIC. E.g:

    with open('unemp.csv', 'rb') as data:
        rows = csv.reader(data, quoting=csv.QUOTE_NONNUMERIC)
        next(rows) #Skip the headers.
        floats = [[item for number, item in enumerate(row) if item and (1 <= number <= 12)] for row in rows]
    
    print(floats)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some difficulty passing values and arrays between this two functions Here's the
I'm having some difficulty with this example: <img src=image1/<?php echo $file; ?>.jpg style=width:500px />
I'm having some difficulty writing this code in php. I want to create a
I'm having some difficulty figuring out the best ways to pause and resume my
I having some difficulty implementing a context menu into my android application. My first
I am having some difficulty locating information of comparing C strings. I understand that
I'm having difficulty converting some NSOperation code to ARC. My operation object uses a
I'm having some difficulty trying to get Jackson to serialize/deserialize JSON date strings sent
I'm having some difficulty including a directory into the jar maven is creating. I
I am having some difficulty with these two functions: byteArrayToInt and intToByteArray . The

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.