Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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_Python 3.x_File_Text - Fatal编程技术网

将大文本文件与python结合起来

将大文本文件与python结合起来,python,python-3.x,file,text,Python,Python 3.x,File,Text,我有一堆文本文件,它们有下一种格式 word(1) num(1,1) num(1,2) num(1,3) ... num(1,300) word(2) num(2,1) num(2,2) num(2,3) ... num(2,300) word(3) num(3,1) num(3,2) num(3,3) ... num(3,300) ... word(n) num(n,1) num(n,2) num(n,3) ... num(n,300) 文件名为palabrasX.txt,其中X是一个数字,

我有一堆文本文件,它们有下一种格式

word(1) num(1,1) num(1,2) num(1,3) ... num(1,300)
word(2) num(2,1) num(2,2) num(2,3) ... num(2,300)
word(3) num(3,1) num(3,2) num(3,3) ... num(3,300)
...
word(n) num(n,1) num(n,2) num(n,3) ... num(n,300)
文件名为palabrasX.txt,其中X是一个数字,表示构成文件中单词的字符串长度(从1到32),因此,例如,如果单词为“cat”,则应位于palabras3.txt中

然后真正的问题是,这些文件中的一些太大了,无法用正常方式打开,我想制作一个包含这些文件所有信息的大文件,所以我会先用1个字符的所有单词,然后用2个字符的所有单词,依此类推,直到32

我正在尝试这样的事情:

# encoding: utf-8
filenames = {}
for i in range(32):
    filenames[i]="palabras"+str(i+1)+".txt"
    with open("VectoresPalabrasEspañol\TodasMisPalabras.txt", "w") as outfile:
        for fname in filenames:
            with open(fname) as infile:
                for line in infile:
                    outfile.write(line)
但它仍然处于工作状态,所以我不确定我的逻辑是否有问题,或者我是否触发了无限循环或其他什么

顺便说一下,完整文件的路径是:

"VectoresPalabrasEspañol\TodasMisPalabras.txt"
palabraX.txt的路径为:

f"palabras\Probables palabras\palabras{length}.txt"
你可以这样编码

这里的文件路径是当前目录

import os

filenames = []

for i in range(32):
    filenames.append("palabras"+str(i+1)+".txt")

with open("TodasMisPalabras.txt", "w") as outfile:
        for fname in filenames:
            # fname=fname[1]
            if os.path.exists(fname):
                with open(fname) as infile:
                    for line in infile:
                        outfile.write(line)

文件太大,无法通过正常方式打开:
SELECT*WHERE word_len=x
x
1-32
范围内。如果我触发了无限循环或其他操作:在…中的fname之后添加一个
print(fname)
,查看您得到了什么,“palabras2.txt”等等,直到32在尝试@safiquislam给出的答案中的代码后,我意识到真正的问题是
filenames[I]
应该包含文件的完整路径字符串,而不仅仅是文件名打印“palabras1.txt”、“palabras2.txt””等等,直到32岁:这不可能!您将获得
dict
键值,该键值为:
0 0 1 0 1 2 0….
OP希望排序“首先是包含1个字符的所有单词,然后是包含2个字符的所有单词,依此类推,直到32”。你的例子在哪里实现了这一点?@stovfl如果我理解ans是。。。。。。文件名数组在附加所有文件名之后包含排序('palabras1.txt'、'palabras2.txt'、…)。。。。。。。因此,在文件名循环中,在写入“palabras1.txt”时,文件数据将首先写入一个大文件,因此每个文件都包含
word(1)、word(2)、word(n)
,OP希望在这个大文件中:
word(1)、word(1)、word(1)…
从所有32个文件中依次是
word(2)、word(2)、word(2)…
等等。我有一个愚蠢的问题,你的代码中的注释有实际作用吗?最近,我发现一些注释的行为类似于活动代码,如#encoding:utf-8@MiguelFernandoMaciasMacias不,你可以移除它