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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:07:58+00:00 2026-06-14T10:07:58+00:00

I have a CSV file, here is a sample of what it looks like:

  • 0

I have a CSV file, here is a sample of what it looks like:

Year:  Dec: Jan:
1      50   60
2      25   50
3      30   30
4      40   20
5      10   10

I know how to read the file in and print each column (for ex. – ['Year', '1', '2', '3', etc]). But what I actually want to do is read the rows, which would be like this ['Year', 'Dec', 'Jan'] and then ['1', '50', '60'] and so on.

And then I would like to store those numbers ['1', '50', '60'] into variables so I can total them later for ex.:

Year_1 = ['50', '60']. Then I can do sum(Year_1) = 110.

How would I go about doing that in Python 3?

  • 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-14T10:07:59+00:00Added an answer on June 14, 2026 at 10:07 am

    You could do something like this:

    with open("data1.txt") as f:
        lis = [line.split() for line in f]        # create a list of lists
        for i, x in enumerate(lis):              #print the list items 
            print "line{0} = {1}".format(i, x)
    
    # output 
    line0 = ['Year:', 'Dec:', 'Jan:']
    line1 = ['1', '50', '60']
    line2 = ['2', '25', '50']
    line3 = ['3', '30', '30']
    line4 = ['4', '40', '20']
    line5 = ['5', '10', '10']
    

    or :

    with open("data1.txt") as f:
        for i, line in enumerate(f):             
            print "line {0} = {1}".format(i, line.split())
    
    # output         
    line 0 = ['Year:', 'Dec:', 'Jan:']
    line 1 = ['1', '50', '60']
    line 2 = ['2', '25', '50']
    line 3 = ['3', '30', '30']
    line 4 = ['4', '40', '20']
    line 5 = ['5', '10', '10']
    

    Edit:

    with open('data1.txt') as f:
        print "{0}".format(f.readline().split())
        for x in f:
            x = x.split()
            print "{0} = {1}".format(x[0],sum(map(int, x[1:])))
    
    # output          
    ['Year:', 'Dec:', 'Jan:']
    1 = 110
    2 = 75
    3 = 60
    4 = 60
    5 = 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv file, and I want to extract the each column a
I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I currently have a csv file that I'm parsing with an example from here:
Little Background: I have csv file which has lots of rows and each row
i have csv files, java app and database, i read csv file from my
How to read and import .csv file in groovy on grails. I have .csv
I have a CSV file that goes something like this: ['Name1', '', '', '',
I have CSV file with sample data in this form : 220 30 255
I have data in a CSV file. One of the column lists a persons
I have my CSV file imported as such: records = FasterCSV.read(path, :headers => true,

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.