在python中读取多个JSONL文件

2024-09-30 18:15:47 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有几个相同布局的Jsonl文件 Folder

我想在文件夹中循环读取每个文件并合并到一个文件中。 在我使用的代码下面,它一直在运行

a=[]
for filepath in glob.iglob('C:\\Users\\txb1025\\Desktop\\Perso\\Challenges\\Daltix\\Data\\*.jsonl'):
    for line in open(filepath, 'r'):
        a.append(json.loads(line))```
any help would be very appreciated


Tags: 文件代码in文件夹forline布局folder
1条回答
网友
1楼 · 发布于 2024-09-30 18:15:47

您可以使用fileinput模块:

import fileinput

with fileinput.input(files=("file1", "file2")) as varname:

https://docs.python.org/3/library/fileinput.html &燃气轮机;读取每个文件并将其放入一个文件中

我从来没有迭代过我的文件夹并将多个文件放入其中,但您可能可以迭代该文件夹,将文件夹中的所有文件放入列表并传递该列表

我希望这就是你想要的

相关问题 更多 >