Python-打开所有txt文件,删除空行并查找特定字符
我想做的是: a) 打开目录中的所有文件(在本例中:长篇故事中的章节) b) 删除所有空行 c) 查找以“-”开头的句子(本例中为对话) 我能够创建运行良好的代码,但只针对一个文件:Python-打开所有txt文件,删除空行并查找特定字符,python,Python,我想做的是: a) 打开目录中的所有文件(在本例中:长篇故事中的章节) b) 删除所有空行 c) 查找以“-”开头的句子(本例中为对话) 我能够创建运行良好的代码,但只针对一个文件: file = open('.\\stories\\test\\01.txt', 'r', encoding="utf-16 LE") string_with_empty_lines = file.read() lines = string_with_empty_lines.split(&qu
file = open('.\\stories\\test\\01.txt', 'r', encoding="utf-16 LE")
string_with_empty_lines = file.read()
lines = string_with_empty_lines.split("\n")
non_empty_lines = [line for line in lines if line.strip() != ""]
string_without_empty_lines = ""
for line in non_empty_lines:
if line.startswith('- '):
string_without_empty_lines += line + "\n"
print(string_without_empty_lines)
我开始混淆这一点,因为我有很多文件,我想全部打开并打印所有文件的结果(可能将所有结果保存到一个文件中,但现在不需要)。新代码的第一部分成功地打开了文件(用注释的打印行检查),但当我添加编辑部分时,什么也没有发生(我甚至在控制台中没有错误)
在一个文件的例子中,如果有更多的文件,在您可以说smt like之前 对于dir:with open(file)….中的文件,请记住您还必须更改目标文件
with open('source.txt') as source:
with open('target.txt','w') as target:
for line in source.readlines():
l = line.strip('\n')
# identify if the 1st char is '-'
if l[0] == '-':
# do somethin e.g. add 'dialog' at the beginning...
# skip empty line
if len(l) == 0:
continue
#Rewrite to target file
target.write(l + '\n')
target.close()
source.close()
在一个文件的例子中,如果有更多的文件,在您可以说smt like之前 对于dir:with open(file)….中的文件,请记住您还必须更改目标文件
with open('source.txt') as source:
with open('target.txt','w') as target:
for line in source.readlines():
l = line.strip('\n')
# identify if the 1st char is '-'
if l[0] == '-':
# do somethin e.g. add 'dialog' at the beginning...
# skip empty line
if len(l) == 0:
continue
#Rewrite to target file
target.write(l + '\n')
target.close()
source.close()
如果源文件位于
源目录
中,并且希望在目标目录
中输出目标文件,则可以这样做:
import os
import path
source_dir = "source_dir"
target_dir = "target_dir"
# on linux or mac, you can get filenames in the specific dir.
# not sure what will happen on Windows
filenames = os.listdir(source_dir)
for filename in filenames:
# get full path of source and target file
filepath_source = path.join(source_dir, filename)
filepath_target = path.join(target_dir, filename)
# open source file and target file
with open(filepath_source) as f_source, open(filepath_target, 'w') as f_target:
for line in f_source:
if len(line.strip()) == 0:
continue
if line[0] == '-':
# do something
f_target.write(line)
如果源文件位于
源目录
中,并且希望在目标目录
中输出目标文件,则可以这样做:
import os
import path
source_dir = "source_dir"
target_dir = "target_dir"
# on linux or mac, you can get filenames in the specific dir.
# not sure what will happen on Windows
filenames = os.listdir(source_dir)
for filename in filenames:
# get full path of source and target file
filepath_source = path.join(source_dir, filename)
filepath_target = path.join(target_dir, filename)
# open source file and target file
with open(filepath_source) as f_source, open(filepath_target, 'w') as f_target:
for line in f_source:
if len(line.strip()) == 0:
continue
if line[0] == '-':
# do something
f_target.write(line)
您希望发生什么,代码的哪一部分应该实现这一点?对于以“-”开头的行,您希望发生什么?它们会保存在新文件或其他对象中吗?“c)从保存在目录下的所有148.txt文件中,在控制台行中查找以“-”开头的句子(在本例中为:dialogs)“Print”。这些文件很乱,因为格式不好,有很多空行,所以我也需要删除它们。正如我所说的,第一个代码很好,但只有当我指向特定的文件时才有效。我正在设法打开所有文件并打印结果。我自己读这些文件中的对话(它们是一本书中的章节)。编写一个函数,以您希望的方式处理单个文件,然后使用该函数处理所有文件。您希望发生什么,代码的哪一部分应该实现这一点?对于以“-”开头的行,您希望发生什么?它们会保存在新文件或其他对象中吗?“c)从保存在目录下的所有148.txt文件中,在控制台行中查找以“-”开头的句子(在本例中为:dialogs)“Print”。这些文件很乱,因为格式不好,有很多空行,所以我也需要删除它们。正如我所说的,第一个代码很好,但只有当我指向特定的文件时才有效。我正在设法打开所有文件并打印结果。我自己读这些文件中的对话(它们是一本书中的章节)。编写一个函数,以您希望的方式处理单个文件,然后使用该函数处理所有文件。