# Find a value between two of the same characters in a string using Python

I am attempting to make a python script that imports a given file.

Then on the import of the file, I am pulling a value from in the filename to determine what function to run.

I will then eventually extend this to pull all files from a folder rather than explicitly pass it a specific file.

The format of the file names is always the following:

blah_Value_blah.extension


I am wondering if there is a better and more efficient way to pull Value from the above example than what I have given below?

Here is my code:

from os.path import splitext, basename

under = '_'

base = basename(splitext(filename_goes_here))
value = base[base.find(under)+len(under):base.rfind(under)]


I am aware I can merge my two lines of code above into one line but it would be very unsightly.

Examples of the filenames are:

//path/to/file/GAME_team_2017.csv
//path/to/file/GAME_player_2017.csv
//path/to/file/GAME_rules_2017.csv


The sample output of the above files would be:

'team'
'player'
'rules'

• Why not make this a function? With good defaults, and an example usage. It'd make the question smaller to read, and not look like example code. – Peilonrayz Nov 14 '17 at 17:26

Rather than using str.find, you could better describe yourself using regex. However it's not that much of an improvement.

For example using the regex _(.+)_ on the basename of the file is all you need. If you think a file extension is going to have an _ then you may need the splitext.

This can get:

from os.path import splitext, basename
from re import search

base = basename(splitext(filename_goes_here))
value = search('_(.+)_', base)
if value is not None:
value = value.group(1)


If you're using Python 3.6, as noted in the comments by 200_success, you could change the last line to:

value = value

• Why not simplify the last line to value = value.group(1)? – 200_success Nov 14 '17 at 22:08
• In Python 3.6, you could also write value = value. – 200_success Nov 14 '17 at 22:18

Since you said the format of the file names is always blah_Value_blah.extension, I would simply split the name at _ and access the value at index 1. For example, 'GAME_player_2017.csv'.split('_')

If you have a list like this

filenames = ['GAME_team_2017.csv',
'GAME_player_2017.csv',
'GAME_rules_2017.csv']


I would split each string and get item at index 1 with a list comprehension.

values = [name.split('_') for name in filenames]


To make the code reusable, I would turn it into a function using listdir() from os module:

from os import listdir

def get_values(path_to_folder):
filenames = listdir(path_to_folder)
values = [name.split('_') for name in filenames]
return values


Now you could call the function with a path as an argument and based on the returned values you could determine what function to run.

For example:

values = get_values(path_to_folder)

for value in values:
# determine what function to run


import os
{"ft_lang_label":"__label__en","ft_lang_prob":0.82462686,"math_prob":0.61502504,"size":976,"snap":"2019-51-2020-05","text_gpt3_token_len":236,"char_repetition_ratio":0.108024694,"word_repetition_ratio":0.0,"special_character_ratio":0.2397541,"punctuation_ratio":0.098958336,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95930964,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-19T20:31:42Z\",\"WARC-Record-ID\":\"<urn:uuid:b89e3acf-932c-409b-8a0c-c1a879917916>\",\"Content-Length\":\"152226\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fbf05b80-19d6-455f-94f1-f26a899c9fe0>\",\"WARC-Concurrent-To\":\"<urn:uuid:ee16342d-2f8e-4b54-b0b1-1bf0c771a3a4>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/180422/find-a-value-between-two-of-the-same-characters-in-a-string-using-python\",\"WARC-Payload-Digest\":\"sha1:P4RL33TPME5H3CW6WYJHSSFOIK3NCJW3\",\"WARC-Block-Digest\":\"sha1:7V5QTV6CZA5M4WDDAJF5OGLD5RLDYNPV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250594705.17_warc_CC-MAIN-20200119180644-20200119204644-00497.warc.gz\"}"}