[Distutils] Reading in Universal Newline mode with pkg_resources

P.J. Eby pje at telecommunity.com
Wed Feb 9 18:52:01 CET 2011


At 08:37 PM 2/8/2011 +0200, cool-RR wrote:
>Hello,
>
>I'm accessing a text file using `pkg_resources`. I want to read it 
>in universal newline mode, i.e. to read newlines as '\n' regardless 
>whether they're actually '\r\n'. How do I do this?

Perhaps:

   StringIO(resource_string('whatever','resource').replace('\r\n','\n'))

?

Or, this might be better, since I believe splitlines uses a true 
Universal Newline approach:

   StringIO('\n'.join(resource_string('whatever','resource').splitlines()))

The pkg_resources APIs are binary, rather than text, so you have to 
do the necessary munging yourself, unless you use resource_filename() 
and open the file directly (which has its own issues).



More information about the Distutils-SIG mailing list