简体   繁体   English

将大文本文件与 python 合并

[英]combine big text files with python

I have a bunch of text files that have the next format我有一堆具有下一个格式的文本文件

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)

The name of the files is palabrasX.txt where X is a number that represents the length of the strings that makes the words inside the files (which go from 1 to 32), so for example if the word is "cat" then should be inside palabras3.txt文件的名称是 palabrasX.txt,其中 X 是一个数字,表示在文件中生成单词的字符串的长度(其中 go 从 1 到 32),例如,如果单词是“cat”,那么应该是palabras3.txt 里面

Then the real problem is that some of these files are too big to open by normal means, and I want to make one big file that has all the information of these files, so I would have all the words with 1 character first then all the words with 2 and so on until 32那么真正的问题是这些文件中的一些太大而无法通过正常方式打开,我想制作一个包含这些文件所有信息的大文件,所以我会先拥有所有带有 1 个字符的单词,然后是所有带 2 的单词,依此类推,直到 32

I'm trying something like this:我正在尝试这样的事情:

# 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)

but it stays in a working state, so I'm not sure if I have a problem with the logic, or if I triggered an infinite loop or something else.但它停留在工作 state 中,所以我不确定我的逻辑是否有问题,或者我是否触发了无限循环或其他什么。

by the way, the path for the full file is:顺便说一句,完整文件的路径是:

"VectoresPalabrasEspañol\TodasMisPalabras.txt"

and the path for the palabraX.txt is: palabraX.txt 的路径是:

f"palabras\Probables palabras\palabras{length}.txt"

you can code this way你可以这样编码

here file path is current directory这里文件路径是当前目录

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)

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

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