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 8245905

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:20:16+00:00 2026-06-07T22:20:16+00:00

I am parsing a csv file where the first line is the header. I

  • 0

I am parsing a csv file where the first line is the header.
I want to sum the amount column according to dates, but am getting an error message.
To debug I am checking if the column is a digit as well as if it is a string according to the error message – and it is both.
What could be the reason for this?

def parseDataFromFile(self,f):
    fh = open(f,'r')
    s = 0
    for line in fh:
        #parsing the line according to comma and stripping the '\n' char
        year,month,day,amount = line.strip('\n').split(',')

        #checking the header row, could check if was first row as well - would be faster
        if (amount == "Amount"): continue

        #just for the debug checks
        #here is the question

        if isinstance(amount,str):
            print "amount is a string"
            #continue
        if amount.isdigit:
            print "amount is a digit"

        #sum on the amount column
        s = s + amount

Output:
amount is a string
amount is a digit
amount is a string
amount is a digit

Error:

s = s + amount 
TypeError: unsupported operand type(s) for +: 'int' and 'str'
  • 0 0 Answers
  • 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-07T22:20:18+00:00Added an answer on June 7, 2026 at 10:20 pm

    Your problem is that s is an integer, you initialize it to 0. Then you try to add a string to it. amount is always a string. You do nothing to turn your number-like data into actual numbers, it will always be a string.

    If you expect amount to be a number, then use:

    s += float(amount)
    

    PS: you should use the csv module in the stdlib for reading CSV files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I parse my CSV file without parsing first line ? This class
I am parsing a big csv file and I am using the first line
I'm parsing a CSV file in which each line look something as below. 10998,4499,SLC27A5,Q9Y2P5,GO:0000166,GO:0032403,GO:0005524,GO:0016874,GO:0047747,GO:0004467,GO:0015245,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
I have this code for parsing a CSV file. var query = from line
I am parsing a CSV file using fgetcsv, specifically using $line_of_text. I want to
I wrote a script parsing a .csv file in groovy using tokenize, which ended
I'm trying to create a simple Silverlight application that involves parsing a CSV file
I currently have a csv file that I'm parsing with an example from here:
I'm reading in from a CSV file, parsing it, and storing the data, pretty
I'm working on a project that involves parsing a large csv formatted file in

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.