如何进入目录中的所有文本文件并删除它们中的第二个空格后的所有内容?

huangapple go评论72阅读模式
英文:

How do I go into all text files in a directory and delete everything in them after the second space?

问题

I have a directory like this:
如何进入目录中的所有文本文件并删除它们中的第二个空格后的所有内容?

每个文本文件都包含以下信息:

106 114 24 25 1 0
705 79 19 21 1 0
661 361 30 37 1 0
212 332 30 37 1 0
704 236 20 25 1 0
620 404 30 37 1 0
615 248 20 25 1 0
641 165 20 25 1 0
676 47 19 21 1 0

我正在尝试编写一个脚本(在Windows 11上),该脚本进入该目录,访问每个文本文件,并删除第二个空格之后的所有内容。

基本上,我希望新的文本文件如下所示:

106 114
705 79
661 361
212 332
704 236
620 404
615 248
641 165
676 47

该脚本也可以使用Python编写。

英文:

I have a directory like this:
如何进入目录中的所有文本文件并删除它们中的第二个空格后的所有内容?

Each text file has this information:

106 114 24 25 1 0
705 79 19 21 1 0
661 361 30 37 1 0
212 332 30 37 1 0
704 236 20 25 1 0
620 404 30 37 1 0
615 248 20 25 1 0
641 165 20 25 1 0
676 47 19 21 1 0

I am trying to write a script (windows 11) that goes into this directory, accesses each text file and deletes everything after the second space.

Basically, I want the new text files to be like this:

106 114
705 79
661 361
212 332
704 236
620 404
615 248
641 165
676 47

The script can be in python as well

答案1

得分: 4

import os

directory = '/path/to/directory'

for filename in os.listdir(directory):
if filename.endswith('.txt'):
with open(os.path.join(directory, filename), 'r+') as file:
lines = file.readlines()
file.seek(0)
for line in lines:
words = line.split()
new_line = ' '.join(words[:2]) + '\n'
file.write(new_line)
file.truncate()

英文:
import os

directory = '/path/to/directory'

for filename in os.listdir(directory):
    if filename.endswith('.txt'):
        with open(os.path.join(directory, filename), 'r+') as file:
            lines = file.readlines()
            file.seek(0)
            for line in lines:
                words = line.split()
                new_line = ' '.join(words[:2]) + '\n'
                file.write(new_line)
            file.truncate()

Replace /path/to/directory with the path to your directory

答案2

得分: 0

这应该可以运行:

import os

DIR = '/path';

for listed_file in os.listdir(DIR):
    if listed_file.endswith(".txt"):
        with open(listed_file, 'r') as file:
            final_lines = []
            for line in file.readlines():
                new_line = " ".join(line.split()[:2]) + "\n"
                final_lines.append(new_line)
        file = open(listed_file, 'w')
        file.writelines(final_lines)

它将遍历DIR中的每个文件,检查它是否是一个txt文件,然后获取它的行并使用新的分割行重写它。

英文:

This should work:

import os

DIR = '/path'

for listed_file in os.listdir(DIR):
    if listed_file.endswith(".txt"):
        with open(listed_file, 'r') as file:
            final_lines = []
            for line in file.readlines():
                new_line = " ".join(line.split()[:2]) + "\n"
                final_lines.append(new_line)
        file = open(listed_file, 'w')
        file.writelines(final_lines)

it will run through each and every file in DIR, check if it is a txt file and then get its lines and rewrite it with the new split lines.

答案3

得分: 0

PowerShell版本:

    Get-ChildItem .\*.txt |Foreach-Object {
        (Select-String -LiteralPath $_ -Pattern ''^\S+\s\S*'').Matches |
        Set-Content -LiteralPath $_
    }
英文:

PowerShell version:

Get-ChildItem .\*.txt |Foreach-Object {
    (Select-String -LiteralPath $_ -Pattern '^\S+\s\S*').Matches |
    Set-Content -LiteralPath $_
}

huangapple
  • 本文由 发表于 2023年3月1日 15:32:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定