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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:39:53+00:00 2026-05-24T03:39:53+00:00

I have a file csv file in this format, I would like to generate

  • 0

I have a file csv file in this format, I would like to generate the average for each step:

elapsed,label
120,Step 01
260,Step 02
113,Step 03
100,Step 01
200,Step 02
103,Step 03

but am having a hard time figuring the lists out. The Python script that I am using is:

for file in sys.argv[1:]:
  for row in csv.DictReader(open(file)):
    label = row['label']
    elapsed = row['elapsed']
    print elapsed,label

I’ve tried

label.append(row['elapsed']) and label.append('elapsed'), but in each case I get this error

AttributeError: 'str' object has no attribute 'append'

I am unsure how to get past this error. I’m new to Python, so perhaps I am missing something in the way lists work? If I can get a list like [120,100] for step 01, I can easily sum and average it, but it is the list creation part that I am stuck on.

Edit: Sum of result list

Now, I have this:

  for item in result:
    for n in result[item]:
      int(n)
      print sum(float(result[item][n]))

but get this error

TypeError: list indices must be integers, not str

But “n” is an integer already, no … ? Or is this referring to “item”? I think I am more confused now than before.

  • 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-05-24T03:39:54+00:00Added an answer on May 24, 2026 at 3:39 am

    If what you want is to make lists,

    from collections import defaultdict
    steps = defaultdict(list)
    
    for file in sys.argv[1:]:
      for row in csv.DictReader(open(file)):
        steps[row['label']].append(row['elapsed'])
    
    averages = dict((key, sum(value)) for key, value in steps.iteritems())
    

    Will give you the averages.

    What you’re doing is attempting to add the string in row['elapsed'] to the string in label, but strings are immutable so can’t be appended to. You can join them with string1 + string2 or `”.join((string1, string2)).

    EDIT: Also, for your new error, See the last line of my code, or, try:

    averages = {}
    for item in result.iteritems():
        print item[0], sum(item[1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV-like text file that has about 1000 lines. Between each record
I have a CSV file that is formatted like: 0.0023709,8.5752e-007,4.847e-008 and I would like
I have a data file in CSV format that has the some data like
I have a csv file of the format: 270291014011 ED HARDY - TRUE TO
I have a basic C# console application that reads a text file (CSV format)
I have a CSV file with several entries, and each entry has 2 unix
I have many different csv files that I would like to convert in to
I have an Excel/CSV file with relatively simple format. 1|2|3|4|10|20|30 | | | |40|50|60
I have a .csv file that is frequently updated (about 20 to 30 times
I have a CSV file that holds about 200,000 - 300,000 records. Most of

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.