Python 如何将大量数据的首字母大写?
我对Python非常陌生,刚出生20-25天,我做了很多研究,虽然我找到了一种使用以下Python代码大写(第一个字母)、名称和地址数据的方法,但我使用“atom editor”编写代码,并使用“Powershell”检查代码 原始数据: 约翰·迪尔 第23号公寓, 第六大道9楼, 新亚区迈克尔街, 加拿大埃德蒙顿。 代码 结果: 约翰·迪尔 第23号公寓, 第六大道9楼, 新亚区迈克尔街, 加拿大埃德蒙顿。 现在假设,如果我必须将“记事本”中的这些名称和地址的首字母大写,一次大写20个,那么我如何才能做到这一点,如何输入数据,如何将数据作为输出返回到“记事本”中。如果有人能指导我,我将不胜感激 这是数据在记事本中的显示方式Python 如何将大量数据的首字母大写?,python,Python,我对Python非常陌生,刚出生20-25天,我做了很多研究,虽然我找到了一种使用以下Python代码大写(第一个字母)、名称和地址数据的方法,但我使用“atom editor”编写代码,并使用“Powershell”检查代码 原始数据: 约翰·迪尔 第23号公寓, 第六大道9楼, 新亚区迈克尔街, 加拿大埃德蒙顿。 代码 结果: 约翰·迪尔 第23号公寓, 第六大道9楼, 新亚区迈克尔街, 加拿大埃德蒙顿。 现在假设,如果我必须将“记事本”中的这些名称和地址的首字母大写,一次大写20个,那
以此类推……如果不想覆盖实际文件的内容,则可以将输出写入新文件。但是,使用相同的文件时,假设文件名为
data.txt
with open('data.txt', 'w+') as f:
data = f.read()
data = data.title()
f.write(data)
这应该行得通
f = open("infile.txt", "r") # change this with your file name, open your source file to read
out = open("outfile.txt", "w") # open your output file to write
for line in f: # read from input line by line
out.write(line.title()) # write to your output line by line
最简单的方法是使用模块。它使就地处理文件变得非常容易,甚至可以选择创建备份文件 以下是如何使用它来解决您的问题:
import fileinput
filename = 'addresses.txt'
backup = '.bak' # will create backup file named 'addresses.txt.bak'
with fileinput.input(filename, inplace=True, backup=backup) as file:
for line in file:
print(line.title(), end='') # end='' suppresses extra newline normally added
print('done')
但是,使用string.title()
方法并不总是能够正常工作,因为它会将类似于“sak's 5th ave”
的内容转换为“sak's 5th ave”
,因为它只将单词识别为任何连续字母组
解决这类问题的一种方法是使用Python的regex(正则表达式)模块(名为)来定义您自己的自定义字模式匹配逻辑
为了说明如何做到这一点,下面是前面修改为使用re
的代码和一个自定义正则表达式模式,它将避免前面提到的两种问题情况:
import re
def titlecase(s):
return re.sub(r"\b[A-Za-z]+('[A-Za-z]+)?",
lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),
s)
filename = 'addresses.txt'
backup = '.bak' # will create backup file named 'addresses.txt.bak'
with fileinput.input(filename, inplace=True, backup=backup) as file:
for line in file:
print(titlecase(line), end='') # end='' suppresses extra newline normally added
print('done')
请标出最佳答案
with open('data.txt', 'w+') as f:
data = f.read()
data = data.title()
f.write(data)
f = open("infile.txt", "r") # change this with your file name, open your source file to read
out = open("outfile.txt", "w") # open your output file to write
for line in f: # read from input line by line
out.write(line.title()) # write to your output line by line
import fileinput
filename = 'addresses.txt'
backup = '.bak' # will create backup file named 'addresses.txt.bak'
with fileinput.input(filename, inplace=True, backup=backup) as file:
for line in file:
print(line.title(), end='') # end='' suppresses extra newline normally added
print('done')
import re
def titlecase(s):
return re.sub(r"\b[A-Za-z]+('[A-Za-z]+)?",
lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),
s)
filename = 'addresses.txt'
backup = '.bak' # will create backup file named 'addresses.txt.bak'
with fileinput.input(filename, inplace=True, backup=backup) as file:
for line in file:
print(titlecase(line), end='') # end='' suppresses extra newline normally added
print('done')