[我试图用“ Wikipedia Extractor(http://medialab.di.unipi.it/wiki/Wikipedia_Extractor)将bz2转换为文本。我下载了带有bz2扩展名的Wikipedia转储,然后在命令行中使用了以下代码行:
WikiExtractor.py -cb 250K -o extracted itwiki-latest-pages-articles.xml.bz2
这给了我可以在链接中看到的结果:
但是,请注意:为了将提取的全部文本合并到一个文件中,可以发出:
> find extracted -name '*bz2' -exec bzip2 -c {} \; > text.xml
> rm -rf extracted
我收到以下错误:
File not found - '*bz2'
我该怎么办?
请仔细阅读。这会有所帮助。
Error using the 'find' command to generate a collection file on opencv
WikiExtractor页面上提到的命令适用于Unix / Linux系统,不适用于Windows。
您在Windows上运行的find
命令与unix / linux中的命令工作方式不同。
只要您使用python前缀运行它,提取的部分在Windows / Linux环境中都可以正常工作。
python WikiExtractor.py -cb 250K -o extracted your_bz2_file
您会看到在与脚本相同的目录中创建了一个extracted
文件夹。
此find
命令应该仅在Linux上才能这样工作。
find extracted -name '*bz2' -exec bzip2 -c {} \; > text.xml
在
extracted
文件夹中找到与bz2和然后在这些文件上执行bzip2
命令并将结果放入text.xml文件。
此外,如果您运行的bzip -help
命令(应该与上面的find
命令一起运行,您会发现它在Windows上不起作用,对于Linux,您将获得以下输出。
gaurishankarbadola@ubuntu:~$ bzip2 -help
bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010.
usage: bzip2 [flags and input files in any order]
-h --help print this message
-d --decompress force decompression
-z --compress force compression
-k --keep keep (don't delete) input files
-f --force overwrite existing output files
-t --test test compressed file integrity
-c --stdout output to standard out
-q --quiet suppress noncritical error messages
-v --verbose be verbose (a 2nd -v gives more)
-L --license display software version & license
-V --version display software version & license
-s --small use less memory (at most 2500k)
-1 .. -9 set block size to 100k .. 900k
--fast alias for -1
--best alias for -9
If invoked as `bzip2', default action is to compress.
as `bunzip2', default action is to decompress.
as `bzcat', default action is to decompress to stdout.
If no file names are given, bzip2 compresses or decompresses
from standard input to standard output. You can combine
short flags, so `-v -4' means the same as -v4 or -4v, &c.
如上所述,bzip2的默认操作是压缩,因此请使用bzcat进行解压缩。
仅在linux上有效的修改后的命令如下所示。
find extracted -name '*bz2' -exec bzcat -c {} \; > text.xml
它可以在我的ubuntu系统上使用。
编辑:
对于Windows:
在尝试任何操作之前,请先阅读说明
WikiExtractor.py
和itwiki-latest-pages-articles1.xml-p1p277091.bz2
(在我的情况下,因为它是一个小文件,我可以找到)。2.在当前目录中打开命令提示符,然后运行以下命令以提取所有文件。
python WikiExtractor.py -cb 250K -o extracted itwiki-latest-pages-articles1.xml-p1p277091.bz2
根据文件大小需要时间,但是现在目录看起来像这样。
注意:如果您已经具有提取的文件夹,请将其移至当前目录,以使其与上面的图像匹配,而不必再次提取。
bz2_Extractor.py
文件中。import argparse
import bz2
import logging
from datetime import datetime
from os import listdir
from os.path import isfile, join, isdir
FORMAT = '%(levelname)s: %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def get_all_files_recursively(root):
files = [join(root, f) for f in listdir(root) if isfile(join(root, f))]
dirs = [d for d in listdir(root) if isdir(join(root, d))]
for d in dirs:
files_in_d = get_all_files_recursively(join(root, d))
if files_in_d:
for f in files_in_d:
files.append(join(f))
return files
def bzip_decompress(list_of_files, output_file):
start_time = datetime.now()
with open(f'{output_file}', 'w+', encoding="utf8") as output_file:
for file in list_of_files:
with bz2.open(file, 'rt', encoding="utf8") as bz2_file:
logger.info(f"Reading/Writing file ---> {file}")
output_file.writelines(bz2_file.read())
output_file.write('\n')
stop_time = datetime.now()
print(f"Total time taken to write out {len(list_of_files)} files = {(stop_time - start_time).total_seconds()}")
def main():
parser = argparse.ArgumentParser(description="Input fields")
parser.add_argument("-r", required=True)
parser.add_argument("-n", required=False)
parser.add_argument("-o", required=True)
args = parser.parse_args()
all_files = get_all_files_recursively(args.r)
bzip_decompress(all_files[:int(args.n)], args.o)
if __name__ == "__main__":
main()
请阅读命令中每个输入的内容。
python bz2_Extractor.py -r extracted -o output.txt -n 10
-r
:您拥有bz2文件的根目录。
-o
:输出文件名
-n
:要写出的文件数。 [如果未提供,它将写出根目录中的所有文件]
注意:我可以看到您的文件以GB为单位,并且有超过500万的文章。如果您尝试使用上述命令将其放在单个文件中,则不确定会发生什么情况,或者您的系统是否可以幸免于难,并且如果确实可以幸免,那么输出文件将是如此之大,因为它是从2.8中提取的GB文件,我认为Windows操作系统无法直接打开它。
所以我的建议是一次处理10000个文件。
让我知道这是否适合您。
PS:对于上述命令,输出看起来像这样。