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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:34:08+00:00 2026-06-07T04:34:08+00:00

I have a txt file containing on each line 2 values separated by a

  • 0

I have a txt file containing on each line 2 values separated by a space:

x1 y1
x2 y2
x3 y3
...
xn yn

I want to get another file containing:

x1 y1
x2 y1+y2
x3 y1+y2+y3
...
xn y1+y2+y3+...+yn

What is the quickest (I mean easiest) way to do this in python ?

  • 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-07T04:34:11+00:00Added an answer on June 7, 2026 at 4:34 am

    This will get you started.

    Given data.txt:

    1 1
    2 2
    3 3
    4 4
    

    and this code snippet:

    with open('data.txt') as inf:
        ysum = 0
        for line in inf:
            line = line.split()
            x, y = [float(i) for i in line]
            ysum += y
            print x, ysum
    

    will give you (using float() above):

    1.0 1.0
    2.0 3.0
    3.0 6.0
    4.0 10.0
    

    On the other hand, if you wanted the lines with the +:

    with open('data.txt') as inf:
        yline = []
        for line in inf:
            line = line.split()
            x = int(line[0])
            yline = '+'.join(yline + [(line[1])])
            print x, yline
            yline = [yline]
    

    will give you (using int() this time):

    1 1
    2 1+2
    3 1+2+3
    4 1+2+3+4
    

    I suspect the above code, esp the 2nd one, could probably be streamlined/optimized more, but should be sufficient to get you started.

    You still will have adjust the conversion from string to appropriate type (float or int) and also create the output file and write to it in the format you prefer. These are details you can best decide.

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

Sidebar

Related Questions

I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have a TXT file containing about 10,000 lines of text. I want to
I have a file words.txt in which each line is a word, followed by
I have a file address.txt containing `0x0003FFB0' at line 1 and column 2 i
I have a txt file containing many records, each record has 5 rows and
i have a .txt file containing data like: He: 22.1 Ar: 21.1 K: 1.22
I have a txt file containing multiple rows of identical size: (Examples) 0123456 789
I have a txt file that created in notepad , containing these lines for
I have a .txt file with product data, which I want to read in
I have text file, names.txt, with every row containing a last name: Smith Johnson

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.