Out of memory while reading excel file

Peter Otten __peter__ at web.de
Fri May 12 16:03:10 EDT 2017


Pavol Lisy wrote:

> On 5/11/17, Peter Otten <__peter__ at web.de> wrote:
>> Mahmood Naderan via Python-list wrote:

>>> between two lines there is a new empty line. In other word, the first
>>> line is the first row of excel file. The second line is empty ("\n") and
>>> the third line is the second row of the excel file.
>>>
>>> Any thought?
>>
>> In text mode Windows translates "\n" to b"\r\n" in the file. Python
>> allows you to override that:

>> So you need to specify newlines:
>>
>> with open(dest, "w", newline="") as outstream:
>>     ...
>>
> 
> But lineterminator parameter (
> https://docs.python.org/3.6/library/csv.html#csv.Dialect.lineterminator
> ) is by default \r\n on linux too!
> 
> b = io.StringIO()
> w = csv.writer(b)
> w.writerows([["a", "b c"], ['a', 'b,c']])
> b.getvalue()  # 'a,b c\r\na,"b,c"\r\n'

I don't have a Windows system to test, but doesn't that mean that on Windows 

with open("tmp.csv", "w") as f:
    csv.writer(f).writerows([["one"], ["two"]])
with open("tmp.csv", "rb") as f:
    print(f.read())

would produce

b"one\r\r\ntwo\r\r\n"

? How is that avoided?




More information about the Python-list mailing list