我正在呼吁 Stack Overflow 的集体智慧,因为我正竭尽全力想弄清楚如何做到这一点,而且我是一名自学成才的编码新手。
我有一个给编辑的信的 txt 文件,我需要将其拆分成各自的文件。
这些文件都以相对相同的方式格式化:
For once, before offering such generous but the unasked for advice, put yourselves in...
Who has Israel to talk to? The cowardly Jordanian monarch? Egypt, a country rocked...
Why is it that The Times does not urge totalitarian Arab slates and terrorist...
PAUL STONEHILL Los Angeles
There you go again. Your editorial again makes groundless criticisms of the Israeli...
On Dec. 7 you called proportional representation “bizarre," despite its use in the...
Proportional representation distorts Israeli politics? Huh? If Israel changes the...
MATTHEW SHUGART Laguna Beach
Was Mayor Tom Bradley’s veto of the expansion of the Westside Pavilion a political...
Although the mayor did not support Proposition U (the slow-growth initiative) his...
If West Los Angeles is any indication of the no-growth policy, where do we go from here?
MARJORIE L. SCHWARTZ Los Angeles
我认为最好的解决方法是尝试使用正则表达式来识别以全大写字母名称开头的行,因为这是真正分辨一个字母在哪里结束和另一个字母在哪里开始的唯一方法。
我尝试了很多不同的方法,但似乎没有一种方法是正确的。我看到的所有其他答案都是基于可重复的行或词。 (例如,此处发布的答案 how to split single txt file into multiple txt files by Python 和 Python read through file until match, read until next pattern )。当我必须调整它以接受我的所有大写单词的正则表达式时,这一切似乎都不起作用。
我设法得到的最接近的是下面的代码。它创建正确数量的文件。但是在创建第二个文件后,一切都出错了。第三个文件是空的,其余所有的文本都是乱序的和/或不完整的。应在文件 4 中的段落在文件 5 或文件 7 等中或完全丢失。
import re
thefile = raw_input('Filename to split: ')
name_occur = []
full_file = []
pattern = re.compile("^[A-Z]{4,}")
with open (thefile, 'rt') as in_file:
for line in in_file:
full_file.append(line)
if pattern.search(line):
name_occur.append(line)
totalFiles = len(name_occur)
letters = 1
thefile = re.sub("(.txt)","",thefile)
while letters <= totalFiles:
f1 = open(thefile + '-' + str(letters) + ".txt", "a")
doIHaveToCopyTheLine = False
ignoreLines = False
for line in full_file:
if not ignoreLines:
f1.write(line)
full_file.remove(line)
if pattern.search(line):
doIHaveToCopyTheLine = True
ignoreLines = True
letters += 1
f1.close()
我愿意完全放弃这种方法并采用另一种方式(但仍然使用 Python)。任何帮助或建议将不胜感激。请假设我是一个没有经验的新手,如果你足够棒花时间帮助我的话。
请您参考如下方法:
我采用了一种更简单的方法并避免使用正则表达式。这里的策略本质上是统计前三个单词中的大写字母,并确保它们通过一定的逻辑。我选择第一个单词是大写的,第二个或第三个单词也是大写的,但您可以根据需要进行调整。然后,这会将每个字母写入与原始文件同名的新文件(注意:它假定您的文件具有 .txt 等扩展名),但附加了一个递增的整数。尝试一下,看看它对您有何帮助。
import string
def split_letters(fullpath):
current_letter = []
letter_index = 1
fullpath_base, fullpath_ext = fullpath.rsplit('.', 1)
with open(fullpath, 'r') as letters_file:
letters = letters_file.readlines()
for line in letters:
words = line.split()
upper_words = []
for word in words:
upper_word = ''.join(
c for c in word if c in string.ascii_uppercase)
upper_words.append(upper_word)
len_upper_words = len(upper_words)
first_word_upper = len_upper_words and len(upper_words[0]) > 1
second_word_upper = len_upper_words > 1 and len(upper_words[1]) > 1
third_word_upper = len_upper_words > 2 and len(upper_words[2]) > 1
if first_word_upper and (second_word_upper or third_word_upper):
current_letter.append(line)
new_filename = '{0}{1}.{2}'.format(
fullpath_base, letter_index, fullpath_ext)
with open(new_filename, 'w') as new_letter:
new_letter.writelines(current_letter)
current_letter = []
letter_index += 1
else:
current_letter.append(line)
我在您的示例输入上对其进行了测试,它运行良好。
