gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:25 25 4
gpt4 key购买 nike

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

  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

最佳答案

因为“德国”确实是“德国\和”。去掉这个词的角色...

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

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

关于python - 更改字符串中的单词以将文本文件大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com