473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Reading the first line of a file (in a zipfile)

Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:

zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?

Apr 11 '07 #1
7 3327
mi**********@gmail.com wrote:
Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:

zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?
You have told split to split on single blank space not whitespace.
To split on whitespace use .split() (e.g. no arguments)

-Larry
Apr 11 '07 #2
En Wed, 11 Apr 2007 16:13:42 -0300, <mi**********@gmail.comescribió:
Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:

zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?
Your code does nothing with the first line on the file; you only split the
*filename* on whitespace. And you extract the file twice.
You don't even try to find "the first occurence of non-whitespace". Surely
an example of file contents and what output you really expect from it
would be adequate.

--
Gabriel Genellina
Apr 11 '07 #3
On Apr 11, 4:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 11 Apr 2007 16:13:42 -0300, <mike.aldr...@gmail.comescribió:


Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:
zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'
The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?

Your code does nothing with the first line on the file; you only split the
*filename* on whitespace. And you extract the file twice.
You don't even try to find "the first occurence of non-whitespace". Surely
an example of file contents and what output you really expect from it
would be adequate.

--
Gabriel Genellina- Hide quoted text -

- Show quoted text -
The file contents have leading whitespace, then a number:
123456 \n
I expect to return '123456'

Apr 11 '07 #4
En Wed, 11 Apr 2007 17:15:48 -0300, <mi**********@gmail.comescribió:
The file contents have leading whitespace, then a number:
123456 \n
I expect to return '123456'
And nothing following the number?

pyline = " 123456 \n"
pyprint line.strip()
123456

--
Gabriel Genellina
Apr 11 '07 #5
On Apr 11, 4:20 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 11 Apr 2007 17:15:48 -0300, <mike.aldr...@gmail.comescribió:
The file contents have leading whitespace, then a number:
123456 \n
I expect to return '123456'

And nothing following the number?

pyline = " 123456 \n"
pyprint line.strip()
123456

--
Gabriel Genellina
That works fine if I am using the interpreter, but I get 'cannot open
file' when i try to read from an archive..
Does that make sense? Sorry, this is my 2nd python script.

Apr 13 '07 #6
En Fri, 13 Apr 2007 08:59:30 -0300, <mi**********@gmail.comescribió:
That works fine if I am using the interpreter, but I get 'cannot open
file' when i try to read from an archive..
Does that make sense? Sorry, this is my 2nd python script.
Try a small, failing example and post the code and the full error
traceback - else it's hard to tell what's happening.

--
Gabriel Genellina
Apr 15 '07 #7
On Apr 11, 1:13 pm, mike.aldr...@gmail.com wrote:
Hi folks,

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?
but I get 'cannot open
file' when i try to read from an archive..
....and that led you to conclude that you cannot split by whitespace?

Apr 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lumpierbritches | last post by:
Thank you in advance for any and all assistance, it is GREATLY appreciated. I was wondering if there is a way to tell Access 97 to compare the first line with other textboxes using the...
4
by: Amit Kulkarni | last post by:
Hi, I have small problem. I want to truncate a line in a text file using C file handling functions and write new line in place of it. How do I do it? e.g. "example.txt" Line 1: This is a...
16
by: shekharpati | last post by:
hi all, I,m trying to read a space delimited file with the use of formated input through fscanf. But surprisingly i am able to read all the 4999 lines of data except the "first line" which is...
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
4
by: tshad | last post by:
I have a program that is reading a .csv file into a dataset and works fine except that it is dropping the first line. I assume that is because it is dropping the header. The problem is the first...
5
by: aboxylica | last post by:
I have a file which contains something like this >ref|NC_001133| CCACACCACACCCACACACCCACACACCACACCACACACCACACCACACCCACACACACA CATCCTAACACTACCCTAACACAGCCCTAATCTAACCCTGGCCAACCTGTCTCTCAACTT...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
2
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
4
by: lilyumestar | last post by:
I have project I have to do for class. We have to write 4 different .java files. Project2.java HouseGUI.java House.java HouseSorting.java I already finish House.java and I need to work on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.