更改字符串中的单词以使文本大写

2024-10-06 11:30:04 发布

您现在位置:Python中文网/ 问答频道 /正文

为了修复一堆全大写的文本文件,我编写了一个脚本:

  1. 降低所有字符并将每行的第一个单词和句点后的第一个单词大写。你知道吗
  2. 将城市和国家名称列表中的所有单词大写(来自另一个文本文件)
def lowit(line):
    line = line.lower()
    sentences = line.split('. ')
    sentences2 = [sentence[0].capitalize() + sentence[1:] for sentence in sentences]
    string2 = '. '.join(sentences2)
    return string2

def capcico(line, allKeywords):
    allWords = line.split(' ')
    original = line.split(' ')

    for i,words in enumerate(allWords):
        words = words.replace(',', '')
        words = words.replace('.', '')
        words = words.replace(';', '')

        if words in allKeywords:
            original[i] = original[i].capitalize()

    return ' '.join(original)

def main():
    dfile = open('fixed.txt', 'w') 
    f = open('allist.txt', 'r')
    allKeywords = f.read().split('\n')

    with open('ulm.txt', 'r') as fileinput:
        for line in fileinput:
            low_line = lowit(line)
            dfile.write('\n' + capcico(low_line, allKeywords))
    dfile.close()

if __name__ == '__main__':
    main()

它是可行的,但问题是,如果同一行中有多个城市/国家,它就不能将其资本化:

TOWN IN WUERTTEMBERG, GERMANY.

更改为:

Town in Wuerttemberg, germany.

有什么问题吗?
TNX公司


Tags: informaindeflineopen单词sentence
2条回答
#Input
fileinput = open("ulm.txt").read()
##Input lower
filow = fileinput.lower()

#Keywords
allKeywords = open("allist.txt").read().split("\n")
for kw in allKeywords:
    filow = filow.replace(kw.strip().lower(), kw.capitalize())

#Dots
fidots = filow.split(".")
for i,d in enumerate(fidots):
    c = d.strip().capitalize()
    dc = d.replace(c.lower(), c)
    fidots[i] = dc

#Result
dfile = open("fixed.txt", "w")
result = ".".join(fidots)
dfile.write(result)
dfile.close()

因为“德国”实际上是“德国\n”。 去掉单词的下线。。。你知道吗

words = words.replace(',', '')
words = words.replace('.', '')
words = words.replace(';', '')

# Add in this line to strip the EOL
words = words.rstrip('\r\n') 

相关问题 更多 >