change the first character of the line to uppercase in a text file

Peter Otten __peter__ at web.de
Sat Jun 27 08:13:07 EDT 2009


Angus Rodgers wrote:

> Yes, I understood that, and it's logical, but what was worrying me
> was how to understand the cross-platform behaviour of Python with
> regard to the different representation of text files in Windows
> and Unix-like OSs. (I remember getting all in a tizzy about this

If you are concerned about line endings open the file in universal newline 
mode:

f = open(filename, "rU")

"""
In addition to the standard fopen values mode may be 'U' or 'rU'. Python is 
usually built with universal newline support; supplying 'U' opens the file 
as a text file, but lines may be terminated by any of the following: the 
Unix end-of-line convention '\n', the Macintosh convention '\r', or the 
Windows convention '\r\n'. All of these external representations are seen as 
'\n' by the Python program. If Python is built without universal newline 
support a mode with 'U' is the same as normal text mode. Note that file 
objects so opened also have an attribute called newlines which has a value 
of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple 
containing all the newline types seen.
"""




More information about the Python-list mailing list