【发布时间】:2022-01-13 15:24:11
【问题描述】:
我有一个文本文件,它是播客的转录。这就是我必须处理的内容的样子,它由一个主机和一个带有时间码的客人组成:
Bill Jacobs 7:22
I love that. I love thinking about it that way, that just keep an eye out for things that people say and be like, is that supported by anything? Because a lot of times it's not. A lot of times it's not. And we take it for granted.
Andy Ward 7:37
Yeah, the one we're most famous for is like blogging takes time. Oh, really? How much time does it take to write a blog post? That's when we have to reach out to 1000 bloggers to get them to fill out a survey. There's literally no other way to have to answer the question. You have to ask people how long they spend writing and then average a bunch of answers together. So the answer is four hours. And we know that because we've asked for seven years in a row. We've asked 1000 bloggers how long they spend writing blog posts, and that's the average.
Bill Jacobs 8:06
That's the average. Okay, so that's interesting. Let's talk about that. Let's talk about that first in the context of what's a good bounce rate, right? In terms of the approach that you have to research, right? How did you, you mentioned you went into the the analytics dashboard, and you kind of extracted that information from there. Is there is there any anything else on that front that that you think we should touch on with regards to the extraction of the data?
我想做的改变是每次以客人或主机名开头的行,我需要格式化该行,使其最终看起来像这样:
**[7:22] Bill Jacobs** I love that. I love thinking about it that way, that just keep an eye out for things that people say and be like, is that supported by anything? Because a lot of times it's not. A lot of times it's not. And we take it for granted.
名称和时间码需要切换,括号中的时间码和两者都要加粗(** **),然后下一行应该只有一个制表符,而是从同一行开始。
我已经创建了 for 循环,每次主机或来宾名称出现在 txt 文件中时,它都能成功获取:
file = open('transcription.txt')
string_list = file.readlines()
host_index = len(host_name)
guest_index = len(guest_name)
for i in range(len(string_list)):
if string_list[i][0:host_index] == host_name:
stripped_line = string_list[i].strip()
line_list = stripped_line.split()
list_of_lists.append(line_list)
我不确定最佳方法是对相应行进行特定编辑并将它们写回文本文件。任何建议将不胜感激。
【问题讨论】:
-
总是TimeStampLine\n 空行\n 转录吗?如果是这种情况,使用换行符可能会更容易拆分。
-
@JeffUK 是的,每一行都用 \n 分割。这样我就可以复制 txt 文件的全部内容,通过换行符拆分,进行更改并能够再次将它们写入文件?
-
如果条目之间只有一个空行,您可以将其用作分隔符。
-
很遗憾,空行不一致。 IE。当说话者有大量文本时,文本被分成段落,因此整个 txt 文件中不存在该序列。 @JeffUK
-
请注意,一旦成绩单以“John Smith , Welcome to the show!”开头,您的方法就会失败。