Organize a text file and covert it to CSV
<p>I have a text file similar to this:</p>

<p>What is the best way of organizing this txt file so that I can convert it to a CSV file later?
Ideally, I would like to have a table at the end with a row for each sequence and its measurements (each measurement in a separate column)</p>

<p>I am  new to python and text editing, any help is appreciated.</p>

<pre><code>AlmostGood = GoodTextFile.readlines()
AlmostGood


['a score=298 EG2=6.4e-70 E=1.3e-83\n',
 's read1067_2d       42 1073 + 1205 TTTTCTAAATTGTAATTTTTATTGGAAAA-CAAA-TATACAACTTGGAAT--GGATTTCCGAGGCAAAATTGTGCCATAAGCAGATTTTAAGTGGCTAAACAA---AGTTTAAA-AGC-AAGTAACAATAAAGAGAAAATGGGTTTCTGGTACAGGACCAGCAGTACAAAATAGTGTACGAGTGACCTGGATAA-TACACCCGTTTCGGCAATAGTGCAATTTAAGT\n',
 's read39_complement 38 1007 + 1149 TTTTTTATAGT-TACTTTTTCTTAGAAACTCAAACTA-ACTGGTCGCAGTCCGGTTTTGCG-G--AAAATTGTGACGGTA-TTCATGTTCTTGCCATCATTGT-AG-AATTC\n',
 '\n',
 'a score=294 EG2=9.1e-69 E=3.1e-82\n',
 's read62_2d         20 1142 + 1186 AGCAGTGGTATCAATGCAGAGATGATTTTTTAAATTGGATTTTTTGATGGAA---CAAA-TATACAACTTGAA-TG-GATTTT\n',
 's read39_complement 14 1098 + 1149 AGCAATCTTGT-AACCCCGATATGGTTTTTTATAGTT-ACTTTTTCTTAGAAACTCAAACTA']
</code></pre>

<p>The quickest way would be to use a regular expression. However, as someone new to Python, please don't get overwhelmed.</p>

<pre><code>import re

matchSequence = []
matchMeasurements = []
for item in AlmostGood:
    matchSequence.append(re.match([a], item))
    matchMeasurements.append(re.match([s], item))
</code></pre>

<p>I can't really test this at the moment, but theoretically it should search through each line in your text, which you've parsed, and sort the two types into separate lists based on the first letter. The strings with newlines should be left out entirely.
<a href="https://docs.python.org/2/library/re.html" rel="nofollow">Here's the regular expression documentation for Python.</a></p>

python csv