top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: when does newlines get set in universal newlines mode?

+2 votes
260 views

I have a text file with Windows-style line terminators (rn) which I open in universal newlines mode (Python 2.7). I would expect the newlines attribute to be set after the first call to the readline() method, but apparently this is not the case:

>>> f=open('test_crlf', 'rU')
>>> f.newlines
>>> f.readline()
'foon'
>>> f.newlines
>>> f.readline()
'barn'
>>> f.newlines
'rn'

On the other hand, the newlines attribute gets set after the first call to readline() on a file with Unix-style line endings.

Is this a bug or a feature?

posted May 4, 2015 by Jai Prakash

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
0 votes

I have a list of a list of integers. The lists are long so i cant really show an actual example of on of the lists, but I know that they contain only the integers 1,2,3,4.

so for example. s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]]

I am calculating the product, sum, max, min.... of each list in s2 but I get negative or 0 for the product for a lot of the lists. (I am doing this in ipython)

for x in s2: Â  Â  print(len = , len(x), sum = , sum(x), prod = , prod(x), max = , max(x), min = , min(x))
...
 (len = , 100, sum = , 247, prod = , 0, max = , 4, min = , 1) (len = , 100, sum = , 230, prod = , -4611686018427387904, max = , 4, min = , 1) (len = , 100, sum = , 261, prod = , 0, max = , 4, min = , 1)
 .....
 (prod =, 0, max =, 4, min =, 1) (prod =, 1729382256910270464, max =, 4, min =, 1) (prod =, 0, max =, 4, min =, 1)
.... 

Whats going on?

+2 votes

I am trying to control Aeroplane mode on Android using Python code. I am running QPyPlus python. When I execute this code(that is widespread in the net),

#!/usr/bin/python 
import android droid = android.Android()
# go to airplane mode
 droid.toggleAirplaneMode()

droid.makeToast('exiting')

I get the error 'no such attribute Android()'.

One important thing is, I want to be able to do this without the need to reboot the Android device and any other working solution is also fine long as it is invokeable through Python.

...