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

时间:2016-11-08 15:39:45

标签: python

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

  1. 降低所有字符并将每行的第一个单词和句点后的第一个单词大写。
  2. 将城市和国家/地区名称列表中的所有字词(来自其他文本文件)大写化
  3. 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()
    

    它有效,但问题是如果同一行中不止一个城市/国家,它就不会资本化:

      

    德国维尔特姆贝格镇。

    更改为:

      

    德国符腾堡州的城镇。

    有什么想法?< TNX

2 个答案:

答案 0 :(得分:1)

这是因为&#34;德国&#34;是真的&#34;德国\ n&#34;。 剥掉EOL ......

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

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

答案 1 :(得分:0)

#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()