Last active
September 5, 2020 17:54
-
-
Save ToshY/a573f6e5a26b77b93e645e1f321e6555 to your computer and use it in GitHub Desktop.
Python simple string incrementer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri May 8 00:07:46 2020 | |
@author: ToishY | |
My favorite online string increment tool "was" dead (http://www.mynikko.com/tools/tool_incrementstr.html), so I made this snippet back then. | |
Haven't tested it fully but seems to work. | |
Note: | |
If there's double quotes in the string, just use a text editor to convert them | |
to single quotes before passing them to the string incrementor | |
""" | |
import sys, getopt, re, datetime | |
def call_main(argv): | |
user_string = '' | |
placeholder = '' | |
start = 0 | |
stop = 0 | |
padding = 0 | |
save_file = 'N' | |
try: | |
opts, args = getopt.getopt(argv,"hi:p:s:e:z:w:",["input_string=","placeholder=","start=","end=","zeropad=","write="]) | |
except getopt.GetoptError: | |
print('HOW TO USE: string_increment.py -i <string_with_placeholder> -p <placeholder> -s <int> -e <int> -z <int> -w <Y|n>') | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print('HOW TO USE: string_increment.py -i <string_with_placeholder> -p <placeholder> -s <int> -e <int> -z <int> -w <Y|n>') | |
sys.exit() | |
elif opt in ("-i", "--input_string"): | |
user_string = str(arg) | |
elif opt in ("-p", "--placeholder"): | |
placeholder = str(arg) | |
elif opt in ("-s", "--start"): | |
start = int(arg) | |
elif opt in ("-e", "--end"): | |
stop = int(arg) | |
elif opt in ("-z", "--zeropad"): | |
padding = int(arg) | |
elif opt in ("-w", "--write"): | |
save_file = str(arg) | |
return user_string, placeholder, start, stop, padding, save_file | |
def string_increment( my_string, placeholder, start, stop, padding=0): | |
""" Increment string """ | |
placeholder = re.escape(placeholder) | |
str_list = [] | |
for i in range(start,stop+1): | |
nmbr = str(i).zfill(padding) | |
str_list.append( re.sub(placeholder,nmbr,my_string) ) | |
return str_list | |
def write_to_text_file( string_list, save_option='Y' ): | |
""" Write to text file """ | |
if(save_option=='Y'): | |
# Save to output file | |
file_name = 'SI_' + datetime.datetime.now().strftime("%d-%m-%Y_%H-%M-%S") + '.txt' | |
with open(file_name, 'w') as f: | |
for item in string_list: | |
f.write("%s\n" % item) | |
print("File was saved as `{}`".format(file_name)) | |
else: | |
# Print to console | |
print('\n'.join(string_list)) | |
if __name__ == "__main__": | |
""" Main string increment """ | |
# Check input | |
my_string, my_placeholder, start, stop, padding, save_it = call_main(sys.argv[1:]) | |
# Increment string | |
result = string_increment(my_string, my_placeholder, start, stop, padding) | |
# Write to text file in executing directory | |
write_to_text_file(result, save_it) | |
# Call from CMD/Terminal | |
$ python string_incrementer.py -i "Hello World [x].mp4" -p "[x]" -s 1 -e 23 -z 2 -w N | |
""" | |
Hello World 01.mp4 | |
Hello World 02.mp4 | |
Hello World 03.mp4 | |
Hello World 04.mp4 | |
Hello World 05.mp4 | |
Hello World 06.mp4 | |
Hello World 07.mp4 | |
Hello World 08.mp4 | |
Hello World 09.mp4 | |
Hello World 10.mp4 | |
Hello World 11.mp4 | |
Hello World 12.mp4 | |
Hello World 13.mp4 | |
Hello World 14.mp4 | |
Hello World 15.mp4 | |
Hello World 16.mp4 | |
Hello World 17.mp4 | |
Hello World 18.mp4 | |
Hello World 19.mp4 | |
Hello World 20.mp4 | |
Hello World 21.mp4 | |
Hello World 22.mp4 | |
Hello World 23.mp4 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment