473,837 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

module: zipfile.writest r - 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_DEF LATED)
dfile = open('LocalCSVF ile.csv', 'r')
zipFile.writest r('test.csv',df ile.read())
dfile.close()
zipFile.close()
*************** *************** *************** *************** *************** *

Aug 14 '07 #1
3 4226
On Aug 14, 11:04 am, towers <damondo...@goo glemail.comwrot e:
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_DEF LATED)
dfile = open('LocalCSVF ile.csv', 'r')
zipFile.writest r('test.csv',df ile.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\n 5,6\r\n'

# Now, create the zipfile
zip_file = zipfile.ZipFile (r'c:\tmp\test. zip', 'w',
zipfile.ZIP_DEF LATED)
zip_file.writes tr('test.csv',c sv_data)
zip_file.close( )

# Now, extract the info
zip_file = zipfile.ZipFile (r'c:\tmp\test. zip')
assert len(zip_file.re ad('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.zi p', 'w',
zipfile.ZIP_DEF LATED)
zip_file.writes tr('test.csv',d file.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...possi bly.

Damon

On 14 Aug, 17:55, "programmer...@ gmail.com" <programmer...@ gmail.com>
wrote:
On Aug 14, 11:04 am, towers <damondo...@goo glemail.comwrot e:
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_DEF LATED)
dfile = open('LocalCSVF ile.csv', 'r')
zipFile.writest r('test.csv',df ile.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\n 5,6\r\n'

# Now, create the zipfile
zip_file = zipfile.ZipFile (r'c:\tmp\test. zip', 'w',
zipfile.ZIP_DEF LATED)
zip_file.writes tr('test.csv',c sv_data)
zip_file.close( )

# Now, extract the info
zip_file = zipfile.ZipFile (r'c:\tmp\test. zip')
assert len(zip_file.re ad('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...@goo glemail.comwrot e:
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.zi p', 'w',
zipfile.ZIP_DEF LATED)
zip_file.writes tr('test.csv',d file.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...possi bly.

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
15241
by: Tung Wai Yip | last post by:
Can I add empty directory using zipfile? When I try to add a directory it complains that it is not a file. tung
1
2927
by: LC | last post by:
Hi, I'm having a problem using the zipfile module in Windows 2000 sp4. When I use it to zip a small file it works fine, but large file doesnt. Here's the error msg i get... --------------------------------------------------------------------- dynamite4_db_archive.py", line 45, in ? file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED) File "C:\Python22\lib\zipfile.py", line 426, in write zinfo.file_size))
3
3755
by: Alan Toppen | last post by:
I was unable to use the ZipFile class in the zipfile module in Python2.4. I got an error that zlib could not be found. Comparing my Python 2.2 installation I noticed Python 2.4 was missing a certain file: /usr/lib/python2.2/lib-dynload/zlibmodule.so. Unable to find a more elegant solution, I copied the file from my Python 2.2 directory into my Python 2.4 directory. When running my Python script it gives a warning: ...
2
4174
by: stewart.midwinter | last post by:
I've been experimenting with the python zipfile module, and have run into a snag. I'm able to create a new zipfile with the module's ZipFile class and add files to it. After closing the file, I'm able to view the contents of it with WinZip. I can also confirm that it is a python zip file by using the is_zipfile() method. All good so far. However, under some circumstances, I am unable to later read the file and extract its contents. ...
11
7625
by: Hari Sekhon | last post by:
I do import zipfile zip=zipfile.ZipFile('d:\somepath\cdimage.zip') zip.namelist() then either of the two: A) file('someimage.iso','w').write(zip.read('someimage.iso'))
1
1774
by: Ritesh Raj Sarraf | last post by:
Hi, I've got a problem here. def compress_the_file(zip_file_name, files_to_compress, sSourceDir): """ Condenses all the files into one single file for easy transfer """ try:
5
2135
by: OriginalBrownster | last post by:
This will probably sound like a very dumb question. I am trying to zip some files within a directory. I want to zip all the files within a directory called "temp" and have the zip archive saved in a directory with temp called ziptemp I was trying to read up on how to use the zipfile module python provides, but I cannot seem to find adequate documentation on function itself.
5
6638
by: Martin | last post by:
I get below error when trying to write unicode xml to a zipfile. zip.writestr('content.xml', content.toxml()) File "/usr/lib/python2.4/zipfile.py", line 460, in writestr zinfo.CRC = binascii.crc32(bytes) # CRC-32 checksum UnicodeEncodeError: 'ascii' codec can't encode character u'\u25cf' in position 2848: ordinal not in range(128) Any ideas?
1
2199
by: John Machin | last post by:
On Jun 4, 8:06 pm, jwesonga <crazylun...@gmail.comwrote: Nothing is ever as it seems. Let's try to work backwards from the error message ... and we don't need your magnificent script, just the traceback will do for now, so: The error says that you are trying to seek 22 bytes backwards from the
0
9846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9693
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10897
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5679
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5859
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4056
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.