Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从python中读取文件_Python_Parsing_Python 3.x_Unicode - Fatal编程技术网

无法从python中读取文件

无法从python中读取文件,python,parsing,python-3.x,unicode,Python,Parsing,Python 3.x,Unicode,您好,我有一个tar文件,其中包含名为0_data、0_index等文件。我正在尝试打开tar文件并读取这些文件的内容。我现在能做的就是提取所有的文件。我无法读取单个文件的内容。我知道它们不是纯文本文件,但是如果我看不到文件的内容,我如何解析这些文件,它们是一堆网页 尝试打开文件时出现的错误是: return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec

您好,我有一个tar文件,其中包含名为
0_data
0_index
等文件。我正在尝试打开tar文件并读取这些文件的内容。我现在能做的就是提取所有的文件。我无法读取单个文件的内容。我知道它们不是纯文本文件,但是如果我看不到文件的内容,我如何解析这些文件,它们是一堆网页

尝试打开文件时出现的错误是:

return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 87: character maps to <undefined>
print(i)
将输出文件名,但
print(content.read())
将给出一个错误:

return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 87: character maps to <undefined>
返回编解码器.charmap\u解码(输入、自身错误、解码表)[0]
UnicodeDecodeError:“charmap”编解码器无法解码位置87:字符映射到的字节0x81

我希望有人能帮助我从文件中读取数据

我不是100%确定这是你的问题,但这至少是一种不好的做法,可能是你问题的根源

您没有关闭任何打开的文件。例如,您有:

my_tarfile=tarfile.open(file)
但在此之后,在打开另一个文件之前,您应该:

my_tarfile.close()
以下是一段引自:

打开的文件会消耗系统资源,根据文件模式,其他程序可能无法访问它们。完成文件后立即关闭它们是很重要的


我的想法是,由于您从未关闭我的文件,系统无法正确读取从中提取的文件。即使这不是问题所在,也最好尽快关闭文件。

我不是100%确定这是您的问题,但这至少是一种不好的做法,可能是您问题的根源

您没有关闭任何打开的文件。例如,您有:

my_tarfile=tarfile.open(file)
但在此之后,在打开另一个文件之前,您应该:

my_tarfile.close()
以下是一段引自:

打开的文件会消耗系统资源,根据文件模式,其他程序可能无法访问它们。完成文件后立即关闭它们是很重要的


我的想法是,由于您从未关闭我的文件,系统无法正确读取从中提取的文件。即使这不是问题所在,也最好尽快关闭文件。

我不是100%确定这是您的问题,但这至少是一种不好的做法,可能是您问题的根源

您没有关闭任何打开的文件。例如,您有:

my_tarfile=tarfile.open(file)
但在此之后,在打开另一个文件之前,您应该:

my_tarfile.close()
以下是一段引自:

打开的文件会消耗系统资源,根据文件模式,其他程序可能无法访问它们。完成文件后立即关闭它们是很重要的


我的想法是,由于您从未关闭我的文件,系统无法正确读取从中提取的文件。即使这不是问题所在,也最好尽快关闭文件。

我不是100%确定这是您的问题,但这至少是一种不好的做法,可能是您问题的根源

您没有关闭任何打开的文件。例如,您有:

my_tarfile=tarfile.open(file)
但在此之后,在打开另一个文件之前,您应该:

my_tarfile.close()
以下是一段引自:

打开的文件会消耗系统资源,根据文件模式,其他程序可能无法访问它们。完成文件后立即关闭它们是很重要的


我的想法是,由于您从未关闭我的文件,系统无法正确读取从中提取的文件。即使不是问题,最好尽快关闭文件。

您需要完整的文件路径来访问它,而不仅仅是名称。 您的第二个函数应该如下所示:

def read_files_nz2(file):
for subdir, dirs, files in os.walk(file):
    for i in files:
        path = os.path.join(subdir, f) # Getting full path to the file
        content=open(path,'r')
        print (content.read())

您需要一个完整的文件路径来访问它,而不仅仅是一个名称。 您的第二个函数应该如下所示:

def read_files_nz2(file):
for subdir, dirs, files in os.walk(file):
    for i in files:
        path = os.path.join(subdir, f) # Getting full path to the file
        content=open(path,'r')
        print (content.read())

您需要一个完整的文件路径来访问它,而不仅仅是一个名称。 您的第二个函数应该如下所示:

def read_files_nz2(file):
for subdir, dirs, files in os.walk(file):
    for i in files:
        path = os.path.join(subdir, f) # Getting full path to the file
        content=open(path,'r')
        print (content.read())

您需要一个完整的文件路径来访问它,而不仅仅是一个名称。 您的第二个函数应该如下所示:

def read_files_nz2(file):
for subdir, dirs, files in os.walk(file):
    for i in files:
        path = os.path.join(subdir, f) # Getting full path to the file
        content=open(path,'r')
        print (content.read())

您需要做以下两件事之一:

  • 打开文件时指定编码:

    # This is probably not the right encoding.
    content = open(path, 'r', encoding='utf-8')
    
    为此,您需要知道文件的编码是什么

  • 以二进制模式打开文件:

    这将导致read返回一个对象而不是字符串,但它将避免任何解码或验证单个字节的尝试


您需要做以下两件事之一:

  • 打开文件时指定编码:

    # This is probably not the right encoding.
    content = open(path, 'r', encoding='utf-8')
    
    为此,您需要知道文件的编码是什么

  • 以二进制模式打开文件:

    这将导致read返回一个对象而不是字符串,但它将避免任何解码或验证单个字节的尝试


您需要做以下两件事之一:

  • 打开文件时指定编码:

    # This is probably not the right encoding.
    content = open(path, 'r', encoding='utf-8')
    
    为此,您需要知道文件的编码是什么

  • 以二进制模式打开文件:

    这将导致read返回一个对象而不是字符串,但它将避免任何解码或验证单个字节的尝试


您需要做以下两件事之一:

  • 打开文件时指定编码:

    # This is probably not the right encoding.
    content = open(path, 'r', encoding='utf-8')
    
    为此,您需要知道文件的编码是什么

  • 以二进制模式打开文件:

    这将导致read返回一个对象而不是字符串,但它将避免任何解码或验证单个字节的尝试

    • 我不确定问题出在哪里 但这件事发生在我身上,它用这种编码解决了

      with open(path, 'r', encoding="ISO-8859-1") as f:
          content = f.read()
      
      另一个好方法是使用UTF-8重写文件,请检查以下代码

      with open(ff_name, 'rb') as source_file:
        with open(target_file_name, 'w+b') as dest_file:
          contents = source_file.read()
          dest_file.write(contents.decode('utf-16').encode('utf-8'))
      
      我不确定问题出在哪里 但这件事发生在我身上,它用这种编码解决了

      with open(path, 'r', encoding="ISO-8859-1") as f:
          content = f.read()
      
      另一个好方法是使用UTF-8重写文件,请检查以下代码

      with open(ff_name, 'rb') as source_file:
        with open(target_file_name, 'w+b') as dest_file:
          contents = source_file.read()
          dest_file.write(contents.decode('utf-16').encode('utf-8'))
      
      我不确定问题出在哪里 但这件事发生在我和他身上