title: python find and match exact string
body: <p>How can I match a string from a column in a <code>.csv</code> file against a list of defined datatypes?</p>
  
  <p>The list of data types is:</p>
  
  <pre><code>Datatype = ["M", "B", "E", "A", "DF", "DW", "DL", "DT", "XTEXT", "MDEDATA"]
  </code></pre>
  
  <p>I want to use the data type that matches the column to look up a value in a dictionary.</p>
  
  <h3>What works</h3>
  
  <p>When my code finds something like <code>DW1</code> in the input (see below for examples) it writes <code>DW:5</code> to the output file, and when it finds <code>DT34</code> it writes <code>DT:7</code>. This works fine.</p>
  
  <h3>What doesn't work</h3>
  
  <p>But the problem is, whenever my code comes across <code>XTEXT</code> it matches it first to <code>E: 2</code> (because <code>E</code> is present in <code>XTEXT</code>) and <em>then</em> to <code>XTEXT: 4105</code> in the next row. As a result, the corresponding line in the output file contains <code>2</code> instead of <code>4105</code>. Something similar happens for <code>MDEDATA</code> which is resolved to <code>A:3</code>.</p>
  
  <p>I tried using a regex to remove numbers from the relevant columns in the input file (this line is commented out in the code above), like this:</p>
  
  <pre><code>if (Datatype[n] == re.sub('[1234567890\n]', '', line.split(";")[1])):
      # ...
  </code></pre>
  
  <p>but this causes problems when encountering strings like <code>X256DATA</code>. </p>
  
  <p>Another issue is that the output file that is produced currently displays <code>abc;DW1</code> in the first column and <code>5</code> in the second column, but I want <code>abc</code> in the first column, <code>DW1</code> in the second and <code>5</code> in the third column!</p>
  
  <p>How can I solve these problems? Any help appreciated.</p>
  
  <hr>
  
  <h3>CSV example</h3>
  
  <p>Input:</p>
  
  <pre><code>1 abc DW1  
  2 uz5 XTEXT
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>1 abc DW1    5
  2 uz5 XTEXT  4105
  </code></pre>
  
  <h3>TXT example</h3>
  
  <p>Input:</p>
  
  <pre><code>abc;DW1\n  
  uz5;XTEXT\n
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>abc;DW1,5\n  
  uz5;XTEXT,4105\n    
  </code></pre>
  
  <h3>Desired output</h3>
  
  <pre><code>abc;DW1;5\n  
  uz5;XTEXT;4105\n
  </code></pre>