One hot vectore

username: Saida2020
cooked: <p>Hello<br>
I have file of sequence of letters ( 8 different letter) at each line. the maximum sequence length is 700.<br>
I would like to create one hot vector for each one . to create one vector</p>
<p>I defined this method</p>
<p>import numpy as np</p>
<p>def one_hot_encode(seq):<br>
dict = {}<br>
mapping = {“B”:0,“L”:1 ,“E”:2, “G”: 3,“H”:4,“I”:5,“S”:6,“T”:7}<br>
print(mapping)<br>
seq2 = [mapping[i] for i in seq]<br>
return np.eye(20)[seq2]</p>
<p>with open(“train_target.txt”) as f, open(‘out_train_target.txt’, ‘w’) as out:<br>
for line in f:<br>
s= line<br>
a = one_hot_encode(line)<br>
print(a)</p>
<p>It give me an error<br>
seq2 = [mapping[i] for i in seq]<br>
KeyError: ‘\n’</p>
<p>Anyone know what that error means</p>

username: InnovArul
cooked: <aside class="quote no-group quote-modified" data-username="Saida2020" data-post="1" data-topic="109554">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saida2020/48/53003_2.png" class="avatar"> Saida2020:</div>
<blockquote>
<p>mapping = {“A”:0,“C”:1 ,“D”: 2,“E”:3,“F”:4,“G”:5,“H”:6,“I”:7,“K”:8,“L”:9,“M”:10,“N”:11,“P”:12,“Q”:13,“R”:14,“S”:15,“T”:16,“V”:17,“W”:18,“Y”:19}</p>
</blockquote>
</aside>
<p>you do not have mapping for “B”.</p>

username: Saida2020
cooked: <p>Yes thanks<br>
I edited the question , since I wrote something wrong , the error is different</p>

username: InnovArul
cooked: <aside class="quote no-group" data-username="Saida2020" data-post="1" data-topic="109554">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saida2020/48/53003_2.png" class="avatar"> Saida2020:</div>
<blockquote>
<p>KeyError: ‘\n’</p>
</blockquote>
</aside>
<p>you can use <code>.strip()</code> function to remove new line characters.</p><aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/9347419/python-strip-with-n">
  <header class="source">

      <a href="https://stackoverflow.com/questions/9347419/python-strip-with-n" target="_blank" rel="noopener">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/1218883/mallard-duck" target="_blank" rel="noopener">
    <img alt="Mallard Duck" src="https://www.gravatar.com/avatar/8e4b3d88c58ba1011f655348c8ea64af?s=256&amp;d=identicon&amp;r=PG" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/9347419/python-strip-with-n" target="_blank" rel="noopener">Python strip with \n</a>
</h4>

<div class="tags">
  <strong>python, strip</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/1218883/mallard-duck" target="_blank" rel="noopener">
    Mallard Duck
  </a>
  on <a href="https://stackoverflow.com/questions/9347419/python-strip-with-n" target="_blank" rel="noopener">07:07AM - 19 Feb 12 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>