简体   繁体   English

如何合并文件中的内容?

[英]How to merge the content from files?

I have a list of files in a folder, the format of the file name as follows我有一个文件夹中的文件列表,文件名的格式如下

coronavirus-tweet-id-2020-06-01-00.txt
coronavirus-tweet-id-2020-06-01-01.txt
coronavirus-tweet-id-2020-06-01-02.txt
coronavirus-tweet-id-2020-06-01-00.txt
coronavirus-tweet-id-2020-06-02-01.txt
coronavirus-tweet-id-2020-06-02-02.txt

Inside the file, data is in the format在文件内部,数据格式为

1279067346301321217
1279067346209038337
1279067346343038977
1279067346301333507

I wanted to merge all these files to one file(according to date) so the final output should look like我想将所有这些文件合并到一个文件中(根据日期),所以最终的 output 应该看起来像

coronavirus-tweet-id-2020-06-01.txt
coronavirus-tweet-id-2020-06-02.txt

Then I wanted to collect 1000 sample the content from the final the file.然后我想从最终文件中收集 1000 个样本内容。

I am not getting any clue to merge the file.我没有任何合并文件的线索。

The answer to this question can be found in some other questions but the jist of it is below.这个问题的答案可以在其他一些问题中找到,但它的要点如下。

root_filename = "coronavirus-tweet-id-2020-06-01.txt"

with open(root_filename, "w") as root_file:
    for i in range(0, 99): # or whatever you file quantity is
        with open(f"coronavirus-tweet-id-2020-06-01-{i:02}.txt", "r") as temp:
            root_file.write(temp.read())

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

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