How to read files with separators in Python and append characters at the end?
<p>I have a file format that looks like this</p>

<pre><code> &gt;1ATGC&gt;2TTTT&gt;3ATGC&gt;$$$&gt;B1ATCG&gt;B2TT-G&gt;3TTCG&gt;B4TT-G&gt;B5TTCG&gt;B6TTCG$$$&gt;C1TTTT&gt;C2ATGC
</code></pre>

<p>Note: "$$$" divides the file, such that anything before $$$ is Set 1 and after $$$ is Set 2 and after the next $$$ Set3 etc.</p>

<p>I have to do the following:</p>

<p>a. Concatenate the sequences following ">". So, I have to join "ATGC" "TTTT" "ATGC" and store in (1) and I have to concatenate "ATCG" "TT-G" "TTCG" "TT-G" "TTCG""TTCG" and store as (2)... concatenate again and store in (3)</p>

<p>The output should be a list that looks like:</p>

<pre><code>("ATGCTTTTATGC","ATCGTT-GTTCGTT-GTTCGTTCG","TTTTATGC")
</code></pre>

<p>(2) Then, I find the the Set that has the maximum length => here Set(2)</p>

<p>(3) If length of Set i is not equal to Set (2), then I add "Z" at the end Set i, such that length of Set i is now equal to length of Set (2)</p>

<p>(4) I replace all "-" with "Z"</p>

<p>The output should look like:</p>

<pre><code> ("ATGCTTTTATGCZZZZZZZZZZZZ",
 "ATCGTTZGTTCGTTZGTTCGTTCG",
 "TTTTATGCZZZZZZZZZZZZZZZZ")
</code></pre>

<p>Here is the code, I have attempted:</p>

<pre><code>in_file = open('c:/test.txt','r')
org = []
seqlist = []
seqstring = ""

for line in in_file:
    if line.startswith("$$$"):
         if seqstring!= "":
            seqlist.append(seqstring)
            seqstring = ""
         org.append(line.rstrip("\n"))
    elif line.startswith("&gt;"):
        seqstring += line.rstrip("\n")
seqlist.append(seqstring)

setdraft = seqlist
maxsetlength = max(len(seqlist))

setdraft2 =[]  

for i in setdraft:
     if len(i) != maxsetlength:
         setdraft2 = i.append("Z")

setfinal =[]

for j in setdraft2:
     if j in setdraft2 =="-":
         setfinal = j.insert ("Z")
</code></pre>

<p>The above script does not work. It gives me multiple errors. 
Eg. When I print <code>setdraft</code> it gives me the output</p>

<pre><code>['&gt;1ATGC&gt;2TTTT&gt;3ATGC&gt;$$$&gt;B1ATCG&gt;B2TT-G&gt;3TTCG&gt;B4TT-G&gt;B5TTCG&gt;Bââ6TTCG$$$&gt;C1TTTT&gt;C2ATââGC']
</code></pre>

<p>which is the same as the input </p>

<pre><code>Traceback (most recent call last):
  File "C:/Users/ACER/Desktop/trial.py", line 25, in &lt;module&gt;
    maxsetlength = max(len(seqlist))
TypeError: 'int' object is not iterable
</code></pre>

<p>It's unclear how fragile your data set is, but if it follows the pattern above (namely the last 4 characters are the ones you are looking for) then you can use a couple of <code>split()</code>s and <code>itertools.zip_longest</code> and <code>zip</code> it back to append the <code>Z</code></p>

<pre><code>&gt;&gt;&gt; import itertools as it
&gt;&gt;&gt; import string
&gt;&gt;&gt; def digit_index(s):
...     for i, c in enumerate(s):
...         if c in string.digits:
...             return i
...     return 0
...
&gt;&gt;&gt; s = '&gt;1ATGC&gt;2TTTT&gt;3ATGC&gt;$$$&gt;B1ATCG&gt;B2TT-G&gt;3TTCG&gt;B4TT-G&gt;B5TTCG&gt;B6TTCG$$$&gt;C1TTTT&gt;C2ATGC'
&gt;&gt;&gt; parsed = [''.join(y[digit_index(y)+1:].replace('-', 'Z') for y in x.split('&gt;')) for x in s.split('$$$')]
&gt;&gt;&gt; parsed
['ATGCTTTTATGC', 'ATCGTTZGTTCGTTZGTTCGTTCG', 'TTTTATGC']
&gt;&gt;&gt; [''.join(x) for x in zip(*it.zip_longest(*parsed, fillvalue='Z'))]
['ATGCTTTTATGCZZZZZZZZZZZZ',
 'ATCGTTZGTTCGTTZGTTCGTTCG',
 'TTTTATGCZZZZZZZZZZZZZZZZ']
</code></pre>

<p>If you don't mind it as a list then you can avoid <code>join()</code>ing it back to a string:</p>

<pre><code>&gt;&gt;&gt; list(zip(*it.zip_longest(*parsed, fillvalue='Z')))
[('A', 'T', 'G', 'C', 'T', 'T', 'T', 'T', 'A', 'T', 'G', 'C', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z'), 
 ('A', 'T', 'C', 'G', 'T', 'T', 'Z', 'G', 'T', 'T', 'C', 'G', 'T', 'T', 'Z', 'G', 'T', 'T', 'C', 'G', 'T', 'T', 'C', 'G'),
 ('T', 'T', 'T', 'T', 'A', 'T', 'G', 'C', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z')]
</code></pre>

python append startswith