472,142 Members | 1,050 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

module: zipfile.writestr - line endings issue


Hi

I'm probably doing something stupid but I've run into a problem
whereby I'm trying to add a csv file to a zip archive - see example
code below.

The csv just has several rows with carriage return line feeds (CRLF).

However after adding it to an archive and then decompressing the line
endings have been converted to just line feeds (LF).

Does anyone know if this is a bug or am I just doing something wrong?

Many Thanks,
Damon

************************************************** **************************
import zipfile
zipFile = zipfile.ZipFile('LocalZipFile.zip', 'a',
zipfile.ZIP_DEFLATED)
dfile = open('LocalCSVFile.csv', 'r')
zipFile.writestr('test.csv',dfile.read())
dfile.close()
zipFile.close()
************************************************** **************************

Aug 14 '07 #1
3 4090
On Aug 14, 11:04 am, towers <damondo...@googlemail.comwrote:
Hi

I'm probably doing something stupid but I've run into a problem
whereby I'm trying to add a csv file to a zip archive - see example
code below.

The csv just has several rows with carriage return line feeds (CRLF).

However after adding it to an archive and then decompressing the line
endings have been converted to just line feeds (LF).

Does anyone know if this is a bug or am I just doing something wrong?

Many Thanks,
Damon

************************************************** **************************
import zipfile

zipFile = zipfile.ZipFile('LocalZipFile.zip', 'a',
zipfile.ZIP_DEFLATED)
dfile = open('LocalCSVFile.csv', 'r')
zipFile.writestr('test.csv',dfile.read())
dfile.close()
zipFile.close()
************************************************** **************************
Line endings will drive you up the wall. Anyway, the python zipfile
library does not seem to be doing any translations. The code below
works for me.

# begin code
import zipfile
import os.path

# First create some work data...
csv_data = '1,2\r\n3,4\r\n5,6\r\n'

# Now, create the zipfile
zip_file = zipfile.ZipFile(r'c:\tmp\test.zip', 'w',
zipfile.ZIP_DEFLATED)
zip_file.writestr('test.csv',csv_data)
zip_file.close()

# Now, extract the info
zip_file = zipfile.ZipFile(r'c:\tmp\test.zip')
assert len(zip_file.read('test.csv')) == len(csv_data)
# end code

Something else must be tweaking your line endings.
HTH!

jw

Aug 14 '07 #2
Thanks - your code works for me also.

But I still get the issue when I read the file directly and add it to
the archive.

Say if I:

1. Use the test.csv file created with your code - currently the line
endings look good (viewed in notepad on Win XP)
2. Run the following code:

# begin code
import zipfile
import os.path

# Now, create the zipfile
dfile = open('test.csv', 'r')
zip_file = zipfile.ZipFile(r'C:\temp\ice\line endings\test.zip', 'w',
zipfile.ZIP_DEFLATED)
zip_file.writestr('test.csv',dfile.read())
dfile.close()
zip_file.close()

3. Then extract the file and the file endings have been corrupted. Now
one long line in notepad. (Other programs interpret correctly though.)

Maybe the issue lies with this way (i.e. dfile.read()) of writing the
file to the archive...possibly.

Damon

On 14 Aug, 17:55, "programmer...@gmail.com" <programmer...@gmail.com>
wrote:
On Aug 14, 11:04 am, towers <damondo...@googlemail.comwrote:
Hi
I'm probably doing something stupid but I've run into a problem
whereby I'm trying to add a csv file to a zip archive - see example
code below.
The csv just has several rows with carriage return line feeds (CRLF).
However after adding it to an archive and then decompressing the line
endings have been converted to just line feeds (LF).
Does anyone know if this is a bug or am I just doing something wrong?
Many Thanks,
Damon
************************************************** **************************
import zipfile
zipFile = zipfile.ZipFile('LocalZipFile.zip', 'a',
zipfile.ZIP_DEFLATED)
dfile = open('LocalCSVFile.csv', 'r')
zipFile.writestr('test.csv',dfile.read())
dfile.close()
zipFile.close()
************************************************** **************************

Line endings will drive you up the wall. Anyway, the python zipfile
library does not seem to be doing any translations. The code below
works for me.

# begin code
import zipfile
import os.path

# First create some work data...
csv_data = '1,2\r\n3,4\r\n5,6\r\n'

# Now, create the zipfile
zip_file = zipfile.ZipFile(r'c:\tmp\test.zip', 'w',
zipfile.ZIP_DEFLATED)
zip_file.writestr('test.csv',csv_data)
zip_file.close()

# Now, extract the info
zip_file = zipfile.ZipFile(r'c:\tmp\test.zip')
assert len(zip_file.read('test.csv')) == len(csv_data)
# end code

Something else must be tweaking your line endings.
HTH!

jw

Aug 14 '07 #3
On Aug 14, 1:32 pm, towers <damondo...@googlemail.comwrote:
Thanks - your code works for me also.

But I still get the issue when I read the file directly and add it to
the archive.

Say if I:

1. Use the test.csv file created with your code - currently the line
endings look good (viewed in notepad on Win XP)
2. Run the following code:

# begin code
import zipfile
import os.path

# Now, create the zipfile
dfile = open('test.csv', 'r')
zip_file = zipfile.ZipFile(r'C:\temp\ice\line endings\test.zip', 'w',
zipfile.ZIP_DEFLATED)
zip_file.writestr('test.csv',dfile.read())
dfile.close()
zip_file.close()

3. Then extract the file and the file endings have been corrupted. Now
one long line in notepad. (Other programs interpret correctly though.)

Maybe the issue lies with this way (i.e. dfile.read()) of writing the
file to the archive...possibly.

Damon
Please don't top post.

The problem is with how you are opening the file. You need to open in
binary mode if you wish to read your file unaltered. Also, file() is
preferred over open() these days I think. Use:

dfile = file('test.csv', 'rb')

--
Paul Carter

Aug 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tung Wai Yip | last post: by
1 post views Thread by LC | last post: by
3 posts views Thread by Alan Toppen | last post: by
2 posts views Thread by stewart.midwinter | last post: by
11 posts views Thread by Hari Sekhon | last post: by
1 post views Thread by Ritesh Raj Sarraf | last post: by
5 posts views Thread by OriginalBrownster | last post: by
1 post views Thread by John Machin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.