total.txt文本文件部分内容如下: img\0003-17fb\00000000imgfront.jpg 0,272.0,391.0,377.0,448.0…
- User
total.txt文本文件部分内容如下: img\0003-17fb\00000000_img_front.jpg 0,272.0,391.0,377.0,448.0 0,558.0,393.0,584.0,417.0 0,420.0,404.0,495.0,442.0 0,598.0,399.0,681.0,459.0 0,511.0,403.0,529.0,418.0 0,391.0,402.0,424.0,413.0 0,523.0,397.0,562.0,423.0 0,433.0,402.0,457.0,409.0 1,424.0,395.0,430.0,418.0 1,419.0,396.0,425.0,416.0 1,52.0,386.0,68.0,442.0 1,765.0,395.0,771.0,416.0 1,203.0,408.0,211.0,433.0 1,40.0,412.0,52.0,452.0 1,771.0,390.0,780.0,423.0 1,265.0,392.0,279.0,426.0 1,34.0,386.0,57.0,445.0 1,429.0,395.0,434.0,414.0 1,1166.0,411.0,1193.0,473.0 img\0003-17fb\00000010_img_front.jpg 0,308.0,396.0,412.0,458.0 0,591.0,397.0,618.0,423.0 0,444.0,411.0,523.0,453.0 0,635.0,404.0,718.0,467.0 0,542.0,409.0,565.0,425.0 0,428.0,409.0,461.0,420.0 0,557.0,401.0,596.0,429.0 1,454.0,404.0,462.0,422.0 1,449.0,404.0,455.0,423.0 1,71.0,400.0,96.0,455.0 1,800.0,399.0,807.0,418.0 1,234.0,419.0,245.0,444.0 1,96.0,424.0,111.0,461.0 1,825.0,397.0,844.0,441.0 1,490.0,404.0,495.0,410.0 1,56.0,401.0,88.0,458.0 1,462.0,404.0,468.0,417.0 1,1263.0,417.0,1279.0,482.0 1,741.0,408.0,746.0,415.0 img\0003-17fb\00000020_img_front.jpg 0,396.0,392.0,502.0,457.0 0,689.0,394.0,717.0,421.0 0,512.0,407.0,606.0,458.0 0,747.0,401.0,837.0,468.0 2,752.0,404.0,757.0,413.0 0,633.0,405.0,662.0,423.0 0,534.0,405.0,558.0,414.0 0,654.0,397.0,694.0,427.0 1,550.0,400.0,557.0,407.0 1,179.0,395.0,200.0,448.0 1,859.0,398.0,866.0,415.0 1,340.0,413.0,348.0,438.0 1,228.0,418.0,240.0,452.0 1,977.0,392.0,997.0,444.0 1,585.0,401.0,591.0,409.0 1,508.0,400.0,515.0,422.0 1,158.0,395.0,189.0,450.0 1,559.0,400.0,564.0,407.0 1,508.0,400.0,517.0,423.0 img\0003-17fb\00000030_img_front.jpg 0,846.0,402.0,866.0,429.0 0,418.0,389.0,545.0,466.0 0,759.0,393.0,789.0,422.0 0,502.0,409.0,642.0,480.0 0,844.0,401.0,948.0,473.0 2,826.0,405.0,831.0,414.0 0,690.0,405.0,721.0,426.0 0,604.0,405.0,638.0,413.0 0,719.0,397.0,764.0,430.0 1,611.0,399.0,618.0,409.0 1,987.0,398.0,994.0,407.0 1,208.0,392.0,229.0,438.0 1,948.0,398.0,956.0,422.0 1,393.0,414.0,400.0,439.0 1,292.0,418.0,305.0,452.0 1,1136.0,391.0,1175.0,452.0 1,189.0,393.0,212.0,438.0 1,621.0,399.0,628.0,415.0 1,576.0,399.0,584.0,408.0。 遍历文件的每一行,将诸如img\0003-17fb\00000000_img_front.jpg内容修改成0003-17fb_00000000_img_front.jpg,python
- Assistant
可以使用以下Python代码实现这个任务:
with open("total.txt", "r") as f: lines = f.readlines() with open("total_modified.txt", "w") as f: for line in lines: img_path, *coords = line.strip().split(" ") new_img_path = img_path.replace("\\", "_").replace("img", "", 1) new_line = f"{new_img_path} {' '.join(coords)}\n" f.write(new_line)这段代码首先打开原始文件并读取所有行,然后创建一个新的文件并对每一行的数据进行修改,将修改后的数据写入新的文件。最后你会在新文件
total_modified.txt中看到修改后的内容。