<p>Here's one way with pure Python:</p>

<pre><code>#!/usr/bin/env python3
import sys
import re

with open (sys.argv[1] , 'r') as fo:
    for line in fo:
        fields = line.split()
        if len(fields)==2:
            print(fields[1],len(fields[1].replace('-','')))
</code></pre>

<p>Running this on your file gives:</p>

<pre><code>$ foo.py file.aln 
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60
</code></pre>

<hr>

<p>Some other options:</p>

<ol>
<li><p><code>awk</code></p>

<pre><code>$ awk 'NF==2{l=$2;gsub(/-/,"",$2); print l,length($2)}' file.aln 
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60
</code></pre></li>
<li><p>Perl</p>

<pre><code>$ perl -lane '$l=$F[1];$l=~s/-//g; $#F==1 &amp;&amp; print "$F[1] ", length($l)' file.aln 
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60
</code></pre></li>
</ol>

CC BY-SA 4.0
2018-09-05T19:12:33.587Z
Well ,Thankyou for this much help, it really solved my problem. Would this also work for a phylip file format?? which is ; [phylip format example] (http://scikit-bio.org/docs/0.2.3/generated/skbio.io.phylip.html)
Sidra Younas
CC BY-SA 4.0
2018-09-05T19:15:15.443Z
@SidraYounas I'm afraid not. This approach assumes that the lines of interest have two fields. Phylip has more. You might want to ask a new question about that.
terdon
CC BY-SA 4.0
2018-09-05T19:27:11.887Z
,alright. But how come I write up these countings into the file and not just printing them out.?? As I'm working with windows and not linux system
Sidra Younas
CC BY-SA 4.0
2018-09-06T05:26:57.683Z
this code does not write the character numbers in my file. Do I need to use file handler to access writing permission to file??
Sidra Younas
CC BY-SA 4.0
2018-09-06T09:56:41.697Z
@SidraYounas yes, that's just normal python. Next time, please make sure to include all your requirements in your question so we don't need to do this sort of back and forth in the comments.
terdon
CC BY-SA 4.0
2018-09-06T10:09:35.077Z
I tried like this:`with open ("sample_clstl_seq.aln" , 'r') as fo:
    for line in fo:
        fields = line.split()
        if len(fields)==2:
            fo.write('{0}{1}'.format(fields[1],len(fields[1].replace('-',''))))` But errors out that it is not writeable.
Sidra Younas
CC BY-SA 4.0
2018-09-06T10:10:23.087Z
I understand, I will be more concerned about this matter.
Sidra Younas
CC BY-SA 4.0
2018-09-07T09:40:24.537Z
@SidraYounas I would not advise modifying the file directly, unless you have some good reason to. It is simpler and less risky to generate a new file and replace the original once you are sure that the result is correct.
bli
CC BY-SA 4.0
2018-09-05T10:57:55.503Z
2018-09-05T11:08:06.647Z
2018-09-05T11:08:06.647Z
terdon
<p>I am using python 3 and biopython 1.72. I have been using a protein sequence file in clustal format.</p>

<pre><code>CLUSTAL 2.1 multiple sequence alignment


Human           ------------------------------------------------------------
Chimpanzee      ------------------------------------------------------------
Dog             ------------------------------------------------------------
Mouse           ------------------------------------------------------------
Xenopus         ------------------------------------------------------------
Amphioxus       MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA


Human           ------------------------------------------------------------
Chimpanzee      ------------------------------------------------------------
Dog             ------------------------------------------------------------
Mouse           ------------------------------------------------------------
Xenopus         ------------------------------------------------------------
Amphioxus       HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK


Human           ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS
Chimpanzee      ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS
Dog             ------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS
Mouse           ------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS
Xenopus         ------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST
Amphioxus       INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD
                                  *:***::  :.  :::     *** **: *        * . 

Human           NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI
Chimpanzee      NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI
Dog             DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL
Mouse           DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL
Xenopus         EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ
Amphioxus       DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH
                :  * *:** :*.* **::.* . **:***** ::* .   .               *  
</code></pre>

<p>I need to iterate over the rows of my file so that I could count the occurrence of characters in each row.
I tried using the code:</p>

<pre><code>file_var = input("File: ")
count = 0
with open (file_var , 'r') as fo:
    for i in fo:
        count += 1
        print (i)
    print (count)
</code></pre>

<p>But this results just counting the total number of rows in a file<code>(count variable)</code>. If I iterate over the file using biopython, it does'nt work also.</p>

<pre><code>for seq_record in SeqIO.parse("sample.aln","clustal"):
    print (seq_record.id)
    print (repr(seq_record.seq))
</code></pre>

<p><strong>seq_record.id</strong> results the names of the sequences used in the file.
<strong>repr(seq_record.seq)</strong> results the sequences of each specie alongwith description.</p>

<p>While I need to</p>

<ol>
<li>get the rows only with amino acids (exclusing the symbols <code>'*,.,:'</code> line) and nothing else from the row and for each row I need to count the number of characters (exclusing <code>'-'</code> dashes)</li>
<li>the number of characters of that row be placed at the end of each of the row.</li>
</ol>

<p>for example: for above example file, the counter should work like;</p>

<pre><code>------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
</code></pre>

CC BY-SA 4.0
2018-09-05T10:37:29.843Z
you can count for the character using 'length' function and before the counting character, use regular expression to remove the other character from the string.
Arijit Panda
CC BY-SA 4.0
2018-09-05T18:16:02.940Z
yes it does, but it won't do for each row separately. Thankyou for the response. :)
Sidra Younas
CC BY-SA 4.0
2018-09-07T09:36:13.287Z
I think biopython is not the right tool if you want the counts row-by-row: biopython treats the sequence of a taxon as one unit, regardless of the original layout in the alignment file.
bli
<python>
Counting the characters in a row using python
# Counting the characters in a row using python

(Asked by: username_0 on 2018-09-05)

I am using python 3 and biopython 1.72. I have been using a protein sequence file in clustal format.
CLUSTAL 2.1 multiple sequence alignment


Human           ------------------------------------------------------------
Chimpanzee      ------------------------------------------------------------
Dog             ------------------------------------------------------------
Mouse           ------------------------------------------------------------
Xenopus         ------------------------------------------------------------
Amphioxus       MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA


Human           ------------------------------------------------------------
Chimpanzee      ------------------------------------------------------------
Dog             ------------------------------------------------------------
Mouse           ------------------------------------------------------------
Xenopus         ------------------------------------------------------------
Amphioxus       HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK


Human           ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS
Chimpanzee      ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS
Dog             ------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS
Mouse           ------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS
Xenopus         ------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST
Amphioxus       INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD
                                  *:***::  :.  :::     *** **: *        * . 

Human           NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI
Chimpanzee      NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI
Dog             DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL
Mouse           DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL
Xenopus         EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ
Amphioxus       DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH
                :  * *:** :*.* **::.* . **:***** ::* .   .               *  

I need to iterate over the rows of my file so that I could count the occurrence of characters in each row.
I tried using the code:
file_var = input("File: ")
count = 0
with open (file_var , 'r') as fo:
    for i in fo:
        count += 1
        print (i)
    print (count)

But this results just counting the total number of rows in a file(count variable). If I iterate over the file using biopython, it does'nt work also.
for seq_record in SeqIO.parse("sample.aln","clustal"):
    print (seq_record.id)
    print (repr(seq_record.seq))

seq_record.id results the names of the sequences used in the file.
repr(seq_record.seq) results the sequences of each specie alongwith description.
While I need to

get the rows only with amino acids (exclusing the symbols '*,.,:' line) and nothing else from the row and for each row I need to count the number of characters (exclusing '-' dashes)
the number of characters of that row be placed at the end of each of the row.

for example: for above example file, the counter should work like;
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42

**Tags:** python
> **Comments:**
> **username_1**: you can count for the character using 'length' function and before the counting character, use regular expression to remove the other character from the string.  
> **username_0**: yes it does, but it won't do for each row separately. Thankyou for the response. :)  
> **username_2**: I think biopython is not the right tool if you want the counts row-by-row: biopython treats the sequence of a taxon as one unit, regardless of the original layout in the alignment file.  

---

## 1 answers

### Answer by username_3 on 2018-09-05. Score: 5 | Accepted answer

Here's one way with pure Python:
#!/usr/bin/env python3
import sys
import re

with open (sys.argv[1] , 'r') as fo:
    for line in fo:
        fields = line.split()
        if len(fields)==2:
            print(fields[1],len(fields[1].replace('-','')))

Running this on your file gives:
$ foo.py file.aln 
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60


Some other options:

awk
$ awk 'NF==2{l=$2;gsub(/-/,"",$2); print l,length($2)}' file.aln 
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60

Perl
$ perl -lane '$l=$F[1];$l=~s/-//g; $#F==1 && print "$F[1] ", length($l)' file.aln 
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60

> **Comments:**
> **username_0**: Well ,Thankyou for this much help, it really solved my problem. Would this also work for a phylip file format?? which is ; [phylip format example] (http://scikit-bio.org/docs/0.2.3/generated/skbio.io.phylip.html)  
> **username_3**: @SidraYounas I'm afraid not. This approach assumes that the lines of interest have two fields. Phylip has more. You might want to ask a new question about that.  
> **username_0**: ,alright. But how come I write up these countings into the file and not just printing them out.?? As I'm working with windows and not linux system  
> **username_0**: this code does not write the character numbers in my file. Do I need to use file handler to access writing permission to file??  
> **username_3**: @SidraYounas yes, that's just normal python. Next time, please make sure to include all your requirements in your question so we don't need to do this sort of back and forth in the comments.  
> **username_0**: I tried like this:`with open ("sample_clstl_seq.aln" , 'r') as fo:     for line in fo:         fields = line.split()         if len(fields)==2:             fo.write('{0}{1}'.format(fields[1],len(fields[1].replace('-',''))))` But errors out that it is not writeable.  
> **username_0**: I understand, I will be more concerned about this matter.  
> **username_2**: @SidraYounas I would not advise modifying the file directly, unless you have some good reason to. It is simpler and less risky to generate a new file and replace the original once you are sure that the result is correct.  

---

