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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:47:09+00:00 2026-05-21T07:47:09+00:00

I have been trying to write to a file,but it kept writing them on

  • 0

I have been trying to write to a file,but it kept writing them on a single line with commas, and square brackets at both ends. How do I write to a file without the square brackets, commas and newline for each rows or lines of the file. Column 6 must remained sorted in descending order.

This is the output from the code below:

[NP_001026855.1, N, 1, YES, 96.4765%, 0.9823825] [NP_597716.1, D, 1, YES, 96.2573%, 0.9812865]

Here’s my codes.

         lines = open("file.txt", "r").readlines()
         outfile = open("file2.txt",'w+')
         lines = [x.split() for x in lines]
         lines.sort(key=lambda x:x[5], reverse=True)
         for i in lines:
             outfile.writelines(i)

The required output should be:

NP_001026855.1 N 1 YES 96.4765% 0.9823825

NP_597716.1    D 1 YES 96.2573% 0.9812865

Thanks guys for your contribution.

  • 1 1 Answer
  • 1 View
  • 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-21T07:47:10+00:00Added an answer on May 21, 2026 at 7:47 am

    Each element of lines is itself an array. Try:

    for i in lines:
        outfile.write(" ".join(i) + "\n")
    

    The .join() method takes an array i and concatenates all the elements together with a space " " between each element. Then a newline "\n" is added to make sure your output is broken up into separate lines.

    Alternately, you don’t even have to save the splitted copy of all the lines:

    lines = open("file.txt", "r").readlines()
    outfile = open("file.2txt", "w")
    lines.sort(key=lambda x: x.split()[5], reverse=True)
    for i in lines:
        outfile.write(i)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been trying to write an awk script which processes a log file, but
I have been trying to write a custom .screenrc file TEST as follows startup_message
I have been trying to write an image on a layer using Quartz but
I have been to trying to write some output to a CSV file using
I have been trying to send a big file over a Socket connection, but
I have been trying to write and read files directly from the BlobStore, but
I have been trying to append strings to a local resource file but I
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been trying to write my own diff3 wrap script for SVN and
I have been trying to write a regex that will remove whitespace following a

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.