Splitting results from chardet output to collect encoding type
<p>I am testing chardet in one of my scripts. I wanted to identify the encoding type of a result variable and chardet seems to do fine here.</p>

<p>So this is what I am doing:</p>

<blockquote>
  <p>myvar1 &lt;-- gets its value from other functions</p>
  
  <p>myvar2 = chardet.detect(myvar1) &lt;-- to detect
  the encoding type of myvar1</p>
</blockquote>

<p>Now when I do a <strong>print myvar2</strong>, I receive the output:</p>

<blockquote>
  <p>{'confidence': 1.0, 'encoding':
  'ascii'}</p>
</blockquote>

<p>Question 1: Can someone give pointer on how to collect only the encoding value part out of this, i.e. ascii.</p>

<p><strong>Edit:</strong>
The scenario is as follows:</p>

<p>I am using unicode(myvar1) to write all input as unicode. But as soon as myvar1 gets a value like 0xab, unicode(myvar1) fails with the error:</p>

<blockquote>
  <p>UnicodeDecodeError: 'ascii' codec
  can't decode byte 0xab in position xxx: ordinal not in range(128)</p>
</blockquote>

<p>Therefore, I am tring to:</p>

<blockquote>
  <ol>
  <li>first identify the encoding type of the input which comes in myvar1, </li>
  <li>take the encoding type in myvar2,</li>
  <li>decode the input (myvar1) with this encoding (myvar2) using decode() [?]</li>
  <li>pass it on to unicode.</li>
  </ol>
</blockquote>

<p>The input coming in is variable and not in my control.</p>

<p>I am sure there are other ways to do this, but I am new to this. And I am open to trying.</p>

<p>Any pointer please.</p>

<p>Many Thanks.</p>
