txt text filtering - python operation

Requirement: If the last column content of a row in the document is 0, delete the row, otherwise keep the row content, and save the filtered content to a new text document.

 

# 读取原始txt文件
with open('depth_values.txt', 'r') as file:
    lines = file.readlines()

# 过滤掉第三列内容为0的行
filtered_lines = [line for line in lines if line.split()[2] != '0']

# 保存到新的txt文件
with open('filtered.txt', 'w') as file:
    file.writelines(filtered_lines)

Guess you like

Origin blog.csdn.net/Scarlett2025/article/details/131661108
Recommended