我有两个文本文件: Speech.txt 和 Script.txt 。 Speech.txt 包含音频文件的文件名列表,而 Script.txt 包含相关的脚本。 Script.txt 包含所有字符和项目的笔录,但是我只希望特定字符的笔录仅。我想编写一个python脚本,将文件名与脚本进行比较,并返回一个文本文件,其中包含文件路径,文件名,扩展名和由 | 分隔的脚本。
语音样本.txt:
ItemsControl
Script.txt示例:
0x000f4a03.wav
0x000f4a07.wav
0x000f4a0f.wav
预期输出:
0x000f4a0f | | And unites the clans against Nilfgaard?
0x000f4a11 | | Of course. He's already decreed new longships be built.
0x000f4a03 | | Thinking long-term, then. Think she'll succeed?
0x000f4a05 | | She's got a powerful ally. In me.
0x000f4a07 | | Son's King of Skellige. Congratulations to you.
代码(正在进行中):
C:/Speech/0x000f4a03.wav|Thinking long-term, then. Think she'll succeed?
C:/Speech/0x000f4a07.wav|Son's King of Skellige. Congratulations to you.
C:/Speech/0x000f4a0f.wav|And unites the clans against Nilfgaard?
以上代码似乎仅适用于Speech.txt的第一行,然后停止。我希望它遍历整个文件,即第2行,第3行...等。我还没有弄清楚如何将结果输出到文本文件中。我目前只能打印出结果。任何帮助将不胜感激!
编辑 链接到Script.txt和Speech.txt。
答案 0 :(得分:1)
您可以使用readlines()方法将这些行加载到列表中,然后对其进行迭代。这样就避免了Kenteep Singh Sidhu正确验证了指针到达文件末尾的问题。
f1=open(r'C:/Speech.txt',"r", encoding='utf8')
f2=open(r'C:/script.txt',"r", encoding='utf8')
lines1 = f1.readlines()
lines2 = f2.readlines()
f1.close()
f2.close()
with open("output.txt","w") as outfile:
for line1 in lines1:
for line2 in lines2:
if line1[0:10]==line2[0:10]:
outfile.write('C:/Speech/' + line2[0:10] + '.wav' + '|' + line2[26:-1],"/n")
答案 1 :(得分:1)
我将把Script.txt
的内容读入字典,然后使用该字典对Speech.txt
中的行进行迭代,仅打印存在的行。这样可以避免多次迭代文件的需要,如果文件很大,这可能会很慢。
演示:
from pathlib import Path
with open("Speech.txt") as speech_file, open("Script.txt") as script_file:
script_dict = {}
for line in script_file:
key, _, text = map(str.strip, line.split("|"))
script_dict[key] = text
for line in map(str.strip, speech_file):
filename = Path(line).stem
if filename in script_dict:
print(f"C:\Speech\{line}|{script_dict[filename]}")
输出:
C:\Speech\0x000f4a03.wav|Thinking long-term, then. Think she'll succeed?
C:\Speech\0x000f4a07.wav|Son's King of Skellige. Congratulations to you.
C:\Speech\0x000f4a0f.wav|And unites the clans against Nilfgaard?
使用With Statement Context Managers打开文件也容易得多,因为您不需要调用.close()
来关闭文件,因为它可以为您处理。
我还使用pathlib.PurePath.stem
从您的.wav
文件中获取文件名。我发现它比os.path.basename
os.path.spltext
函数更易于使用。尽管这是个人喜好,并且一切正常。
如果我们想将输出写入文本文件,可以使用mode="w"
以写入模式打开另一个输出文件:
from pathlib import Path
with open("Speech.txt") as speech_file, open("Script.txt") as script_file, open("output.txt", mode="w") as output_file:
script_dict = {}
for line in script_file:
key, _, text = map(str.strip, line.split("|"))
script_dict[key] = text
for line in map(str.strip, speech_file):
filename = Path(line).stem
if filename in script_dict:
output_file.write(f"C:\Speech\{line}|{script_dict[filename]}\n")
output.txt
C:\Speech\0x000f4a03.wav|Thinking long-term, then. Think she'll succeed?
C:\Speech\0x000f4a07.wav|Son's King of Skellige. Congratulations to you.
C:\Speech\0x000f4a0f.wav|And unites the clans against Nilfgaard?
您可以查看文档中的Reading and Writing Files,以获取有关如何在python中读写文件的更多信息。
答案 2 :(得分:1)
使用max
也是另一种方法,因为这似乎是您的典型联接问题。
pandas
然后只需选择所需的列并将其保存。
import pandas as pd
df = pd.read_csv('speech.txt', header=None, names=['name'])
df1 = pd.read_csv('script.txt', sep='|', header=None, names=['name', 'blank', 'description'])
df1['name'] = df1.name.str.strip() + '.wav'
final = pd.merge(df, df1, how='left', left_on='name', right_on='name')
final['name'] = 'C:/Speech/' + final['name']
print(final)
name blank description
0 C:/Speech/0x000f4a03.wav Thinking long-term, then. Think she'll succeed?
1 C:/Speech/0x000f4a07.wav Son's King of Skellige. Congratulations to you.
2 C:/Speech/0x000f4a0f.wav And unites the clans against Nilfgaard?
答案 3 :(得分:1)
对于Speech.txt
文件的每一行,您需要检查Script.txt
文件中是否存在。考虑到Script.txt
的内容适合存储在内存中,您应该加载其内容以避免每次都读取它。
一旦加载Script.txt
的内容,您只需处理Speech.txt
的每一行,在字典中进行搜索,并在需要时进行打印。
接下来,我提供代码。请注意:
python -O script.py
os.path.splittext(var)[0]
从文件名中删除扩展名strip
处理每行以摆脱空格/换行符。代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# For better print formatting
from __future__ import print_function
# Imports
import sys
import os
#
# HELPER METHODS
#
def load_script_file(script_file_path):
# Parse each line of the script file and load to a dictionary
d = {}
with open(script_file_path, "r") as f:
for transcript_info in f:
if __debug__:
print("Loading line: " + str(transcript_info))
speech_filename, _, transcription = transcript_info.split("|")
speech_filename = speech_filename.strip()
transcription = transcription.strip()
d[speech_filename] = transcription
if __debug__:
print("Loaded values: " + str(d))
return d
#
# MAIN METHODS
#
def main(speech_file_path, script_file_path, output_file):
# Load the script data into a dictionary
speech_to_transcript = load_script_file(script_file_path)
# Check each speech entry
with open(speech_file_path, "r") as f:
for speech_audio_file in f:
speech_audio_file = speech_audio_file.strip()
if __debug__:
print()
print("Checking speech file: " + str(speech_audio_file))
# Remove extension
speech_code = os.path.splitext(speech_audio_file)[0]
if __debug__:
print(" + Obtained filename: " + speech_code)
# Find entry in transcript
if speech_code in speech_to_transcript.keys():
if __debug__:
print(" + Filename registered. Loading transcript")
transcript = speech_to_transcript[speech_code]
if __debug__:
print(" + Transcript: " + str(transcript))
# Print information
output_line = "C:/Speech/" + speech_audio_file + "|" + transcript
if output_file is None:
print(output_line)
else:
with open(output_file, 'a') as fw:
fw.write(output_line + "\n")
else:
if __debug__:
print(" + Filename not registered")
#
# ENTRY POINT
#
if __name__ == '__main__':
# Parse arguments
args = sys.argv[1:]
speech = str(args[0])
script = str(args[1])
if len(args) == 3:
output = str(args[2])
else:
output = None
# Log arguments if required
if __debug__:
print("Running with:")
print(" - SPEECH FILE = " + str(speech))
print(" - SCRIPT FILE = " + str(script))
print(" - OUTPUT FILE = " + str(output))
print()
# Execute main
main(speech, script, output)
调试输出:
$ python speech_transcript.py ./Speech.txt ./Script.txt
Running with:
- SPEECH FILE = ./Speech.txt
- SCRIPT FILE = ./Script.txt
Loaded values: {'0x000f4a03': "Thinking long-term, then. Think she'll succeed?", '0x000f4a11': "Of course. He's already decreed new longships be built.", '0x000f4a05': "She's got a powerful ally. In me.", '0x000f4a07': "Son's King of Skellige. Congratulations to you.", '0x000f4a0f': 'And unites the clans against Nilfgaard?'}
Checking speech file: 0x000f4a03.wav
+ Obtained filename: 0x000f4a03
+ Filename registered. Loading transcript
+ Transcript: Thinking long-term, then. Think she'll succeed?
C:/Speech/0x000f4a03.wav|Thinking long-term, then. Think she'll succeed?
Checking speech file: 0x000f4a07.wav
+ Obtained filename: 0x000f4a07
+ Filename registered. Loading transcript
+ Transcript: Son's King of Skellige. Congratulations to you.
C:/Speech/0x000f4a07.wav|Son's King of Skellige. Congratulations to you.
Checking speech file: 0x000f4a0f.wav
+ Obtained filename: 0x000f4a0f
+ Filename registered. Loading transcript
+ Transcript: And unites the clans against Nilfgaard?
C:/Speech/0x000f4a0f.wav|And unites the clans against Nilfgaard?
输出:
$ python -O speech_transcript.py ./Speech.txt ./Script.txt
C:/Speech/0x000f4a03.wav|Thinking long-term, then. Think she'll succeed?
C:/Speech/0x000f4a07.wav|Son's King of Skellige. Congratulations to you.
C:/Speech/0x000f4a0f.wav|And unites the clans against Nilfgaard?
输出写入文件:
$ python -O speech_transcript.py ./Speech.txt ./Script.txt ./output.txt
$ more output.txt
C:/Speech/0x000f4a03.wav|Thinking long-term, then. Think she'll succeed?
C:/Speech/0x000f4a07.wav|Son's King of Skellige. Congratulations to you.
C:/Speech/0x000f4a0f.wav|And unites the clans against Nilfgaard?