Python Forum
Combining the regex into single findall
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combining the regex into single findall
#1
Hello fellow Python Trekkers,

So, this program is a kind of prelude to a program involving SQLite. I'm not looking for help on that. At least not yet. I want to be able to take some fairly raw data from text files and turn them into rows and columns, as a next step.

But with that in mind, should I be trying to combine all of the findall expressions into a single list of lists, or is the approach I took going to be easier to handle ultimately? Some files that this program will be sorting through when I have the db program working properly may be MB long. So I am worried that if I do add them separately, I will take a big performance hit. Whether or not that's important, I don't really know. I am still very much a beginner.

Without further ado, here is the program:
import re


def test(input_data):
    # extracts strings from the test strings
    input_data = str(input_data)
    f_server = re.findall(r'^(![\w|d]+)', input_data)
    add_server(f_server)
    f_name = re.findall(r'^![\w|d]+\s([a-zA-Z|\(|\)|\'|\"|\&|\.|\s|\d|\w|\-|,]+)', input_data)
    add_file_name(f_name)
    f_size = re.findall('([\d\.]+KB|MB)$', input_data)
    add_file_size(f_size)


def add_server(f_server):
    # adds all server hits to a list
    server_list.append(f_server)


def add_file_name(f_name):
    # adds all file name hits to a list
    f_string = str(f_name[0])
    f_string = f_string.rstrip()
    file_name_list.append(f_string)


def add_file_size(f_size):
    # adds all file sizes to a list
    file_size.append(f_size)


server_list = []
file_name_list = []
file_size = []

# Sample data for testing
test("!FlipMoran 100 Best Science Fiction Novels - David Pringle.txt  ::INFO:: 14.9KB")
test("!dragnbreaker Aldiss, Brian W - SSC 21 - Man in His Time-The Best Science Fiction Of.jpg  ::INFO:: 24.8KB")
test("!dragnbreaker Aldiss, Brian W - SSC 21 - Man in His Time-The Best Science Fiction Of.rtf  ::INFO:: 840.4KB")
test("!pondering42 Bleiler, EF & Dikty, TE - Year's Best Science Fiction Novels - 1953 (html).rar  ::INFO:: 348.0KB")
print("Server list: ", server_list)
print("File name list: ", file_name_list)
print("File size list: ", file_size)
Regards and thanks in advance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  regex findall() returning weird result Radical 1 588 Oct-15-2023, 08:47 PM
Last Post: snippsat
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,173 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  regex.findall that won't match anything xiaobai97 1 1,972 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE
  Regex findall() NewBeie 2 4,235 Jul-10-2020, 12:19 PM
Last Post: DeaD_EyE
  re.findall HELP!!! only returns None Rusty 10 6,813 Jun-20-2020, 12:13 AM
Last Post: Rusty
  The "FindAll" Error BadWhite 6 4,270 Apr-11-2020, 05:59 PM
Last Post: snippsat
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,864 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Issue with re.findall alinaveed786 8 4,767 Oct-20-2018, 09:28 AM
Last Post: volcano63
  [Regex] Findall returns wrong number of hits Winfried 8 5,683 Aug-23-2018, 02:21 PM
Last Post: Winfried
  unable to print the list when using re.findall() satyaneel 5 4,078 Sep-27-2017, 10:26 AM
Last Post: buran

Forum Jump:

User Panel Messages

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