简体   繁体   English

从文本文件中读取列表列表,并加入和解码

[英]Read list of lists from text file and join and decode

I have a list of lists in a text file that contains non encoded unicode. 我在包含未编码的unicode的文本文件中有一个列表列表。 Python is recognising the object being read in as a list without me doing anything like a json.loads() statement direct from the text file. Python无需将我直接从文本文件中执行类似json.loads()语句之类的json.loads()就可以将正在读取的对象识别为列表。

When I try and read all lines of the text file and then do a for loop to iterate through each sublist nothing happens. 当我尝试读取文本文件的所有行,然后执行for循环以遍历每个子列表时,没有任何反应。 When I try encoding the entire object first before attemping to load to JSON nothing also happens. 当我尝试在尝试加载到JSON之前先对整个对象进行编码时,也不会发生任何事情。

Here is my 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')

The input data looks like this: 输入数据如下所示:

[['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']]

The output should be three lines of strings with the above contents encoded. 输出应该是三行字符串,其中上面的内容已编码。 Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错?

Thanks 谢谢

Maybe something like next code snippet? 也许像下一个代码片段一样?

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)

Output : 输出

==> 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']

==>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM