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

  • Home
  • SEARCH
  • 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 9086107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:24:29+00:00 2026-06-16T21:24:29+00:00

if I have a text file like this: [001]This is line 1. [002][too long]This

  • 0

if I have a text file like this:

[001]This is line 1.
[002][too long]This is line 2 but it's Tooooo
oooo long!
[003]This is line 3.

I write a ‘for line in fileA’ to read this file like:

for line in fileA:
    ...

now I need to merge the current line and the next line when line.find(“[too long]”)>=0.
How should I do?

PS:
I wrote:

for line in fileA:
    if line.find("[too long]")>=0:
        loc = fileA.tell()
        fileB = open("file.txt") #open this file again
        fileB.seek(loc)
        line += fileB.readline().strip()

but it did not work. why?

  • 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-16T21:24:30+00:00Added an answer on June 16, 2026 at 9:24 pm

    Sounds too much overhead with extra reading of the file. Try this:

    with open('file.txt') as f:
        for line in f:
            if '[too long]' in line:
                line = line.rstrip('\r\n') + next(f)
            print line
    

    prints

    [001]This is line 1.
    
    [002][too long]This is line 2 but it's Tooooooooo long!
    
    [003]This is line 3.
    

    This appends the following line if [too long] is found in a line. Maybe you want to append all further lines until a line starts with something like [xxx]?

    • 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 that looks like this: Id:001;status:open;Id:002;status:open;Id:003;status:closed; Id:004;status:open;Id:005;status:closed;Id:006;status:open; .... .... Here
if I have a text file like this : this is line one This
i have a text file like this: ********** time1 ********** line of text1 line
I have a log file with content like this: 2012-07-16 03:20:41,23796160897,Text,id:SAR-23796160897-c0-2-1 sub:000 dlvrd:001 submit
I have a plain text file looking like this: some text containing line breaks
I have a text file like this: A B C And each element has
I' ve got a problem with Haskell. I have text file looking like this:
I have a text file looks like this : 100 50 20 90 4.07498
I have a text file that looks like this. A 102 B 456 C
I have a text file that looks like this: value1 value2 value3 There are

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.