Python Forum
fastest way to record values between quotes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fastest way to record values between quotes
#1
Dear All,

I've not been using Python for years (I'm rather a matlab user), But I've the opportunity to work with it again. Blush

The goal is to deal with Ascii files, containing different types of data; I'm wondering how to improve the code here after in order to record specific numbers between quotes, especially can I avoid (hugly) loop use?

Thanks for any advice

Paul
t0 = time.time()
line1 = '"tel 555-669", "duration 6", "number 2.", "Price 3.58"'
quote = re.finditer('"',line1)
position_quote = []
for match_quote in quote:
    position_quote.append(match_quote.span())

Price = float(line1[position_quote[6][0]+7 : position_quote[7][0]-1])
t1 = time.time()
print("Duration : ", t1-t0)
Reply
#2
I ran the program as you posted it and got the same result.
Then I inserted a couple of lines to force a delay.

I inserted the following code immediately after line 6
while t0==time.time():
t0=t0
I also imported time an re.
The program ran and produced the following output Duration : 0.015587329864501953
Your computer will produce a different output because my computer is slower ? faster ? than yours.


import time
import re
t0 = time.time()
line1 = '"tel 555-669", "duration 6", "number 2.", "Price 3.58"'
quote = re.finditer('"',line1)
position_quote = []
for match_quote in quote:
    position_quote.append(match_quote.span())

    while t0==time.time():
        t0=t0
    
Price = float(line1[position_quote[6][0]+7 : position_quote[7][0]-1])
t1 = time.time()
print("Duration : ", t1-t0)

I didn't explain why I inserted the two extra lines of code.
They force a delay to see if the routine is running so fast that there isn't sufficient time for the time function to be updated. So the program just stalls until it is update. Hope this helps.
Reply
#3
Can't you match more directly?
>>> import re
>>> 
>>> line1 = '"tel 555-669", "duration 6", "number 2.", "Price 3.58"'
>>> r = re.search(r"(Price)\s(\d.\d+)", line1)
>>> d = r.groups()
>>> d
('Price', '3.58')
>>> d = dict([d])
>>> d
{'Price': '3.58'}
>>> d['Price']
'3.58'
>>> float(d['Price'])
3.58
Did also match Price so could make a dictionary.
pythonduffer Wrote:Then I inserted a couple of lines to force a delay.
Can use timeit that is made for measure execution of small code snippets.
Reply
#4
@snippsat: thanks for the help; much more "elegant" and efficient than using the loop

Well I need to go deep in regex use
Reply
#5
Hi

An additional question regarding regex use (some things still remain unclear at the moment for me); Ok for:
  • () to create a groupe
  • \s to point to a space character
  • \d+ pour read a number up to the end
  • \d. to allow the dot for a character

But the scheme is pre-defined, I mean the number of space characters is known: so how can I basically test the number of space characters and thus the number of groups?

line2 = '"555 102 4 21", "39 555 6", "555 102"';
reg7 = re.search(r"(\"555)\s(\d+)\s(\d+)\s(\d+)", line2);
d7 = reg7.groups()
Reply
#6
As that string has extra double quotes,can do tricks and match between quotes.
For testing regex look at regex101.
There also a Regular Expression HOWTO and the regular doc.
>>> import re
>>> 
>>> line2 = '"555 102 4 21", "39 555 6", "555 102"'
>>> r = re.findall(r'"(.*?)"', line2)
>>> r
['555 102 4 21', '39 555 6', '555 102']

>>> r[0]
'555 102 4 21'
>>> [int(i) for i in r[0].split()]
[555, 102, 4, 21]
>>> # Or think of why this work,hint new PEP
>>> g = r[0].replace(' ', '_')
>>> g
'555_102_4_21'
>>> int(g)
555102421

# No error using underscore in integer
>>> 100_000_000_123
100000000123
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fastest way tkinter Quatrixouuu 2 349 Feb-19-2024, 07:20 AM
Last Post: Danishhafeez
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  How do I stream and record at the same time with arducam? traderjoe 0 430 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  What is the fastest way to get all the frames from a video file? glorsh66 3 992 May-26-2023, 04:41 AM
Last Post: Gribouillis
  Fastest Way of Writing/Reading Data JamesA 1 2,139 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Two types of single quotes Led_Zeppelin 2 1,863 Mar-15-2021, 07:55 PM
Last Post: BashBedlam
  Only getting last record saved...Why Milfredo 10 4,275 Sep-10-2020, 03:00 AM
Last Post: Milfredo
  Quotes vs. no quotes around numbers Mark17 6 3,071 Aug-06-2020, 04:13 AM
Last Post: t4keheart
  how can we record a video file from our program output (moving object) Zhaleh 0 1,779 Aug-03-2020, 02:47 PM
Last Post: Zhaleh
  Fastest Method for Querying SQL Server with Python Pandas BuJayBelvin 7 6,771 Aug-02-2020, 06:21 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020