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

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

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:

  1. 106 114 24 25 1 0
  2. 705 79 19 21 1 0
  3. 661 361 30 37 1 0
  4. 212 332 30 37 1 0
  5. 704 236 20 25 1 0
  6. 620 404 30 37 1 0
  7. 615 248 20 25 1 0
  8. 641 165 20 25 1 0
  9. 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:

  1. 106 114
  2. 705 79
  3. 661 361
  4. 212 332
  5. 704 236
  6. 620 404
  7. 615 248
  8. 641 165
  9. 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()

英文:
  1. import os
  2. directory = '/path/to/directory'
  3. for filename in os.listdir(directory):
  4. if filename.endswith('.txt'):
  5. with open(os.path.join(directory, filename), 'r+') as file:
  6. lines = file.readlines()
  7. file.seek(0)
  8. for line in lines:
  9. words = line.split()
  10. new_line = ' '.join(words[:2]) + '\n'
  11. file.write(new_line)
  12. file.truncate()

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

答案2

得分: 0

这应该可以运行:

  1. import os
  2. DIR = '/path';
  3. for listed_file in os.listdir(DIR):
  4. if listed_file.endswith(".txt"):
  5. with open(listed_file, 'r') as file:
  6. final_lines = []
  7. for line in file.readlines():
  8. new_line = " ".join(line.split()[:2]) + "\n"
  9. final_lines.append(new_line)
  10. file = open(listed_file, 'w')
  11. file.writelines(final_lines)

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

英文:

This should work:

  1. import os
  2. DIR = '/path'
  3. for listed_file in os.listdir(DIR):
  4. if listed_file.endswith(".txt"):
  5. with open(listed_file, 'r') as file:
  6. final_lines = []
  7. for line in file.readlines():
  8. new_line = " ".join(line.split()[:2]) + "\n"
  9. final_lines.append(new_line)
  10. file = open(listed_file, 'w')
  11. 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

  1. PowerShell版本:
  2. Get-ChildItem .\*.txt |Foreach-Object {
  3. (Select-String -LiteralPath $_ -Pattern ''^\S+\s\S*'').Matches |
  4. Set-Content -LiteralPath $_
  5. }
英文:

PowerShell version:

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

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:

确定