将文本文件内容存储在数组中

13 浏览
0 Comments

将文本文件内容存储在数组中

这个问题已经有答案了:

UnicodeDecodeError: \'charmap\' 编码器无法在位置Y解码字节X: 映射字符到<未定义>

我有一个包含印地文行(大约5400000行)的文本文件。我想将这些行保存在Python的字符串数组中。我尝试了这段代码:

    f = open("cleanHindi_Translated.txt" , "r")
    array = []
    for line in f:
        array.append(line)
    print(array)

但是我遇到了一个错误:

    Traceback (most recent call last):
  File "hindi.py", line 11, in 
    for line in f:
  File "C:\Users\Preeti\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 124: character maps to 
PS C:\Users\Preeti\Downloads\Compressed> python hindi.py
Traceback (most recent call last):
  File "hindi.py", line 11, in 
    for line in f:
  File "C:\Users\Preeti\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 124: character maps to 

我不明白我在这里做错了什么。

admin 更改状态以发布 2023年5月21日
0
0 Comments

'

'lines' 是你要找的数组(列表)

import io
with io.open('my_file.txt','r',encoding='utf-8') as f:
   lines = f.readlines()

'

0