failed to replace tabulator in list elements

Viewed 29

I wanted to replace the tabulator in my results, which was generated and captured by stdout and split into pieces of strings as shown below

the code used to format the results:

Text_formatted = [text.strip() for text in prozess.stdout.split('\n')]

Results are shown in the terminal:

['This is cutadapt 3.4 with Python 3.9.7', 'Command line parameters: --cores=8 -g TAGACATTAAAACTCATGATGGATG...AAAAGRAAGGTATGTGCATCTTA -o 1--2.CA.fasta S1--1.QC.fasta --discard-untrimmed', 'Processing reads on 8 cores in single-end mode ...', 'Finished in 0.81 s (4 µs/read; 13.75 M reads/minute).', '', '=== Summary ===', '', 'Total reads processed: 185,957', 'Reads with adapters:
72,274 (38.9%)', 'Reads written (passing filters): 72,274 (38.9%)', '', 'Total basepairs processed: 25,596,106 bp', 'Total written (filtered): 6,938,162 bp (27.1%)', '', '=== Adapter 3 ===', '', "Sequence: TAGACATTAAAACTCATGATGGATG...AAAAGRAAGGTATGTGCATCTTA; Type: linked; Length: 25+23; 5' trimmed: 72274 times; 3' trimmed: 72274 times", '', '', 'No. of allowed errors:', '1-9 bp: 0; 10-19 bp: 1; 20-25 bp: 2', '', 'No. of allowed errors:', '1-9 bp: 0; 10-19 bp: 1; 20-23 bp: 2', '', "Overview of removed sequences at 5' end", 'length\tcount\texpect\tmax.err\terror counts', '4\t12\t726.4\t0\t12', '5\t250\t181.6\t0\t250', '18\t1\t0.0\t1\t1', '21\t3\t0.0\t2\t1 1 1', '22\t2\t0.0\t2\t0 0 2', '23\t239\t0.0\t2\t7 10 222', '24\t3304\t0.0\t2\t77 3023 204', '25\t67555\t0.0\t2\t66919 592 44', '26\t895\t0.0\t2\t197 677 21', '27\t13\t0.0\t2\t0 5 8', '', '', '', "Overview of removed sequences at 3' end", 'length\tcount\texpect\tmax.err\terror counts', '17\t1\t0.0\t1\t1', '18\t1\t0.0\t1\t1', '19\t5\t0.0\t1\t4 0 1', '20\t12\t0.0\t2\t2 8 2', '21\t71\t0.0\t2\t3 5 63', '22\t1471\t0.0\t2\t51 1397 23', '23\t69654\t0.0\t2\t68917 712 25', '24\t181\t0.0\t2\t29 151 1', '27\t4\t0.0\t2\t4', '28\t449\t0.0\t2\t423 22 4', '29\t166\t0.0\t2\t148 15 3', '30\t237\t0.0\t2\t186 47 4', '31\t22\t0.0\t2\t21 1', '']

But when I used the following code to replace:

for i in Text_formatted:
        i.replace("\t"," ")

It generates, however the same outputs as before. So where could the problem lie on?

Many thanks in advance.

2 Answers

You need to reassign the value of the variable you are replacing:

Text_formatted = [text.strip() for text in prozess.stdout.split('\n')]

for i in range(len(Text_formatted)):
  Text_formatted[i] = Text_formatted[i].replace("\t", "")

I think i iterate through all your letter one by one. So i will never be equal to \t because it's two char. Try to use replace directly on your Text_formatted : Text_formatted.replace('\t',' ')

Related