Read list of lists from text file and join and decode
<p>I have a list of lists in a text file that contains non encoded unicode. Python is recognising the object being read in as a list without me doing anything like a <code>json.loads()</code>statement direct from the text file.</p>

<p>When I try and read all lines of the text file and then do a for loop to iterate through each sublist nothing happens. When I try encoding the entire object first before attemping to load to JSON nothing also happens. </p>

<p>Here is my code:</p>

<pre><code>import glob

myglob =  'C:\\mypath\\*.txt'
myglob = ''.join(myglob)

for name in glob.glob(myglob):

    split_name = name.split('\\')[5]

    with open(name) as f:

        content = f.readlines()

        for xx in content:

            print xx.encode('utf-8')
</code></pre>

<p>The input data looks like this:</p>

<pre><code>[['Alexis S\xe1nchez', 'Alexis', 'S\xe1nchez', 'Forward', 'Arsenal', '13', '25244'],['H\xe9ctor Beller\xedn', 'H\xe9ctor', 'Beller\xedn', 'Defender', 'Arsenal', '13', '125211'],['Libor Koz\xe1k', 'Libor', 'Koz\xe1k', 'Forward', 'Aston Villa', '24', '67285']]
</code></pre>

<p>The output should be three lines of strings with the above contents encoded. Can anyone tell me what I am doing wrong?</p>

<p>Thanks</p>

<p>Maybe something like next code snippet?</p>

<pre><code>listInput = [['Alexis S\xe1nchez', 'Alexis', 'S\xe1nchez', 'Forward', 'Arsenal', '13', '25244'],
  ['H\xe9ctor Beller\xedn', 'H\xe9ctor', 'Beller\xedn', 'Defender', 'Arsenal', '13', '125211'],
  ['Libor Koz\xe1k', 'Libor', 'Koz\xe1k', 'Forward', 'Aston Villa', '24', '67285']]

for listItem in listInput:

    for aItem in listItem:
        aItem = aItem.encode('utf-8')

    print (listItem)
</code></pre>

<p><strong>Output</strong>:</p>

<pre><code>==&gt; python D:\test\Python\39708736.py
['Alexis SÃ¡nchez', 'Alexis', 'SÃ¡nchez', 'Forward', 'Arsenal', '13', '25244']
['HÃ©ctor BellerÃ­n', 'HÃ©ctor', 'BellerÃ­n', 'Defender', 'Arsenal', '13', '125211']
['Libor KozÃ¡k', 'Libor', 'KozÃ¡k', 'Forward', 'Aston Villa', '24', '67285']

==&gt;
</code></pre>

python unicode