I want to scan a file byte for byte for occurences of the the four byte
pattern 0x00000100. I've tried with this:
# start
import sys
numChars = 0
startCode = 0
count = 0
inputFile = sys.stdin
while True:
ch = inputFile.read( 1)
numChars += 1
if len(ch) < 1: break
startCode = ((startCode << 8) & 0xffffffff) | (ord(ch))
if numChars < 4: continue
if startCode == 0x00000100:
count = count + 1
print count
# end
But it is very slow. What is the fastest way to do this? Using some
native call? Using a buffer? Using whatever?
/David 79 5261 pi************@ gmail.com wrote: I want to scan a file byte for byte [...] while True: ch = inputFile.read( 1) [...] But it is very slow. What is the fastest way to do this? Using some native call? Using a buffer? Using whatever?
Read in blocks, not byte for byte. I had good experiences with block
sizes like 4096 or 8192.
-- Gerhard
Okay, how do I do this?
Also, if you look at the code, I build a 32-bit unsigned integer from
the bytes I read. And the 32-bit pattern I am looking for can start on
_any_ byte boundary in the file. It would be nice if I could somehow
just scan for that pattern explicitly, without having to build a 32-bit
integer first. If I could tell python "scan this file for the bytes 0,
0, 1, 0 in succession. How many 0, 0, 1, 0 did you find?"
/David pi************@ gmail.com writes: I want to scan a file byte for byte for occurences of the the four byte pattern 0x00000100. I've tried with this:
use re.search or string.find. The simplest way is just read the whole
file into memory first. If the file is too big, you have to read it in
chunks and include some hair to notice if the four byte pattern straddles
two adjoining chunks.
I'm now down to:
f = open("filename" , "rb")
s = f.read()
sub = "\x00\x00\x01\x 00"
count = s.count(sub)
print count
Which is quite fast. The only problems is that the file might be huge.
I really have no need for reading the entire file into a string as I am
doing here. All I want is to count occurences this substring. Can I
somehow count occurences in a file without reading it into a string
first?
/David
"pi************ @gmail.com" <pi************ @gmail.com> writes: f = open("filename" , "rb") s = f.read() sub = "\x00\x00\x01\x 00" count = s.count(sub) print count
That's a lot of lines. This is a bit off topic, but I just can't stand
unnecessary local variables.
print file("filename" , "rb").read().co unt("\x00\x00\x 01\x00")
--
Björn Lindström <bk**@stp.lingf il.uu.se>
Student of computational linguistics, Uppsala University, Sweden
"pi************ @gmail.com" <pi************ @gmail.com> writes: Which is quite fast. The only problems is that the file might be huge. I really have no need for reading the entire file into a string as I am doing here. All I want is to count occurences this substring. Can I somehow count occurences in a file without reading it into a string first?
How about iterating through the file? You can read it line by line, two lines
at a time. Pseudocode follows:
line1 = read_line
while line2 = read_line:
line_to_check = ''.join([line1, line2])
check_for_desir ed_string
line1 = line2
With that you always have two lines in the buffer and you can check all of
them for your desired string, no matter what the size of the file is.
Be seeing you,
--
Jorge Godoy <go***@ieee.org >
Jorge Godoy <go***@ieee.org > writes: How about iterating through the file? You can read it line by line, two lines at a time. Pseudocode follows:
line1 = read_line while line2 = read_line: line_to_check = ''.join([line1, line2]) check_for_desir ed_string line1 = line2
With that you always have two lines in the buffer and you can check all of them for your desired string, no matter what the size of the file is.
This will fail if the string to search for is e.g. "\n\n\n\n" and it
actually occcurs in the file.
Bernhard
--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/
First of all, this isn't a text file, it is a binary file. Secondly,
substrings can overlap. In the sequence 0010010 the substring 0010
occurs twice.
/David
On 2005-10-28, pi************@ gmail.com <pi************ @gmail.com> wrote: I'm now down to:
f = open("filename" , "rb") s = f.read() sub = "\x00\x00\x01\x 00" count = s.count(sub) print count
Which is quite fast. The only problems is that the file might be huge. I really have no need for reading the entire file into a string as I am doing here. All I want is to count occurences this substring. Can I somehow count occurences in a file without reading it into a string first?
Yes - use memory mapping (the mmap module). An mmap object is like a
cross between a file and a string, but the data is only read into RAM
when, and for as long as, necessary. An mmap object doesn't have a
count() method, but you can just use find() in a while loop instead.
Andrew This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: CHANGE username to westes |
last post by:
What are the most popular, and well supported, libraries of drivers for bar
code scanners that include a Visual Basic and C/C++ API? My requirements
are:
- Must allow an application to be written to a single interface, but support
many different manufacturers' barcode scanning devices. I do not want to
be tied to one manufacturers' software interfaces.
- Must support use of the scanner from Visual Basic, and ideally from C/C++
and...
|
by: Zen |
last post by:
I'm using Access 2000, and I'd like to know if there is a way to use a
scanner (flatbed, doc-feed, etc) to scan forms with OMR or OCR software, and
have the data be automatically (or if not automatically then using a macro
or other means) entered into tables. I guess the real question is do I need
to use an expensive program to do this or is it codable suing Access/VB, and
if it is codable, any suggestions as to how to start?
Many...
|
by: Marie-Christine Bechara |
last post by:
I have a form with a button called btnScan. When i click on this button
i want to scan a file and save it in the database. Any hints?? ideas???
solutions???
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
|
by: Brent Burkart |
last post by:
I am using a streamreader to read a log file into my application. Now I
want to be able to scan for error messages, such as "failed", "error",
"permission denied", so I can take action such as send an email.
I am not quite sure how to approach this as far as scanning the content.
I currently read all of the contents in using the following
Dim contents As String = objStreamReader.ReadToEnd()
|
by: Bob Alston |
last post by:
I am looking for others who have built systems to scan documents, index
them and then make them accessible from an Access database. My
environment is a nonprofit with about 20-25 case workers who use
laptops. They have Access databases on their laptops and the data is
replicated.
The idea is that each case worker would scan their own documents,
either remotely or back at the office.
And NO I am not planning to store the scanned...
| |
by: tshad |
last post by:
We have a few pages that accept uploads and want to scan the files before
accepting them. Does Asp.net have a way of doing a virus scan?
We are using Trendmicro to scan files and email but don't know if we can use
it with our pages to handle files that our clients upload. Is there some
type of API that would allow us to do this?
I want to be able to Upload Word files using:
<input id="MyFile" visible="true" style="width:200px"...
|
by: kirubagari |
last post by:
For i = 49 To mfilesize Step 6
rich1.SelStart = Len(rich1.Text)
rich1.SelText = "Before : " & HexByte2Char(arrByte(i)) & _
" " & HexByte2Char(arrByte(i + 1)) & " " _
& HexByte2Char(arrByte(i + 2)) & " " _
& HexByte2Char(arrByte(i + 3)) & " " _
& HexByte2Char(arrByte(i + 4)) & " " _
|
by: Rotsey |
last post by:
Hi,
I am writing an app that scans hard drives and logs info
about every fine on the drive.
The first iteration of my code used a class and a generic list
to store the data and rhis took 13min on my 60 GB drive.
I wanted it to be quicker.
|
by: iheartvba |
last post by:
Hi Guys,
I have been using EzTwain Pro to scan documents into my access program.
It allows me to specify the location I want the Doc to go to. It also allows me to set the name of the document as well. The link to the program is as below :
EZTwain imaging library system - add TWAIN scanning or image capture to your application.
I'm not sure if it's the nature of the program, but the scanning module is very slow to load. Otherwise it's...
|
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,...
|
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...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |