这是我目前的代码:
with open('Finna.txt') as bigtxt:
for l in bigtxt:
l = l.capitalize()
print l
我尝试在上面列出的文本文件“Finna.txt”上调用上面使用的capitalize()方法
Are there upcoming networking sessions?
Are there walk in hours during the summer?
are there walk in hours today?
Are there walk-in hours during finals?
are there work opportunities for freshmen in engineering career
期望每一行都是大写的,如果它还没有,但唯一返回的是
are there work opportunities for freshmen in engineering career
这是没有资本化的。我在这做错了什么?抱歉,如果这是一个非常基本的问题,我只是开始使用Python。在发布之前,我尝试用我的问题搜索类似的问题。
答案 0 :(得分:2)
使用您提供的代码和文本,输出似乎很好。
但是,我猜你的'Finna.txt'中可能存在一些空格或隐形字符。你可以试试这样的东西 -
with open('finna.txt') as bigtxt:
for l in bigtxt:
l = l.strip().capitalize()
print l
答案 1 :(得分:0)
你可以试试这个:
f = open('Finna.txt')read().splitlines()
new_f = [i[0].upper()+i[1:] for i in f]
输出是现在大写的所有行的列表:
['Are there upcoming networking sessions?', 'Are there walk in hours during the summer?', 'Are there walk in hours today?', 'Are there walk-in hours during finals?', 'Are there work opportunities for freshmen in engineering career']
答案 2 :(得分:0)
您可以使用list-comprehension
和lstrip()/rstrip()
每行,然后capitalize()
和join()
进行打印:
with open('test.txt.', 'r') as f:
print '\n'.join([x.lstrip().rstrip().capitalize() for x in f])
输出:
Are there upcoming networking sessions?
Are there walk in hours during the summer?
Are there walk in hours today?
Are there walk-in hours during finals?
Are there work opportunities for freshmen in engineering career
答案 3 :(得分:0)
简单代码只需用大写字母替换
file = open('Finna.txt).read().splitlines()
for word in file:
st = word.replace(word, word.capitalize())
print(st)
输出将是:
Are there upcoming networking sessions?
Are there walk in hours during the summer?
Are there walk in hours today?
Are there walk-in hours during finals?
Are there work opportunities for freshmen in engineering career