首页 > 解决方案 > 从完整的 txt 文件文件夹创建多个或单个 csv 文件

问题描述

我需要将我的 txt 文件转换为 csv 文件进行注释,以准备我的数据集进行训练。我尝试使用 pandas 进行转换,它使用以下代码适用于单个文件:

import pandas as pd

read_file = pd.read_csv (r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images/annotations/0.txt')
read_file.to_csv (r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images/0.csv', index=None)

但我有大约 4000 个 txt 文件。如何同时转换它们。将它们转换并合并到单个 csv 文件会很好吗?我想使用这些 csv 文件创建 TFRecords。这是我的注释格式以防万一:

15.025299 79.619064 91.971375 27.37761 111.49409 87.36937 120.44259 144.5673 और
195.26416 345.93964 346.07916 195.40369 296.7271 296.54498 411.9508 412.13293 किस
544.8015 579.83813 541.4978 506.46115 42.720642 60.455795 136.19897 118.46382 दिन
275.59427 311.88095 302.1434 265.85672 134.48518 159.5067 173.62825 148.60674 रूप
30.469978 163.88913 164.9093 31.490135 182.98358 181.51782 274.3758 275.84155 इस
33.57235 184.95844 185.26584 33.879738 354.1837 353.62552 436.9943 437.55246 तरह
-2.6164436 45.761616 53.37768 4.9996223 155.51007 137.70114 158.39023 176.19916 साथ
343.8163 512.32336 512.94495 344.4379 134.2903 132.54332 192.49599 194.24297 एकाएक
337.52948 504.81384 505.13098 337.84662 241.95956 240.91986 291.94846 292.98816 रमानाथ
507.9361 555.0499 523.0799 475.96606 4.7673645 56.682793 85.69593 33.780502 गया

It consists of 10 lines, so 10-word level bounding boxes are there. GroundTruth is the last.
x1 x2 x3 x4 y1 y2 y3 y4 order is there.

标签: pythonpandas

解决方案


如果您只想将所有文件转换为 csv,您可以使用 os 模块获取所有文件的列表并遍历它们:

import pandas as pd
import os

origin_path = r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images/annotations'

destination_path = r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images'

for filename in os.listdir(origin_path):
    df = pd.read_csv(os.path.join(origin_path, filename))
    df.to_csv(os.path.join(destination_path, filename.replace('.txt', '.csv')), index=None)

您还可以使用pd.concat将它们组合到单个 csv 中。


推荐阅读