Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python delete double white space between columns from txt file and save result in new file I have txt file 1 has the following contents:

python delete double white space between columns from txt file and save result in new file

I have txt file 1 has the following contents:

AW_003015.1 156 VPSELGGVGLKNTQYARLVEI AS_010029.1 215 LASGETVAAFKLTEPSSGSDA CE_010039.1 433 SEAAWKVTDEKIQIMGGMGFM RN_000019.1 477 ILRLFVALQGKMDKGKELSGL DP_020039.1 603 HPTAQHEKMLKDTWCIEAAAR

I want to make single line space between columns and save that in new txt file as following:

AW_003015.1 156 VPSELGGVGLKNTQYARLVEI AS_010029.1 215 LASGETVAAFKLTEPSSGSDA CE_010039.1 433 SEAAWKVTDEKIQIMGGMGFM RN_000019.1 477 ILRLFVALQGKMDKGKELSGL DP_020039.1 603 HPTAQHEKMLKDTWCIEAAAR

This is my code I did not be able to do that:

outfile = open("file2.txt", "w") # Clears existing file, open for writing

with open("file1.txt") as infile:

for line in infile:

line = line.strip()

#print(line)

if line:

line = line.strip()

while ' ' in line:

line = line.replace(' ', '')

print(line)

outfile.write(line)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions