Hello is there a function that will get a string S and a number n and will add (n-1) # at the start and (n-1) % at the end. In python. I know how to add a char but not a number of them. Example S= abracatabra and n =3 result : ##abracatabra%%
I think this does what you want:
string = "test"
n = 3
for i in range(1,n):
string = "#"+string+"%"
print(string)
The version of #idjaw is better optimized that this solution but remember you can just add strings using +.
Alternative version:
string = "#"*(n-1)+string+"%"*(n-1)
This can also be achieved using Python's Format Specification Mini-Language specification. This allows pad characters and widths to be specified as follows:
n = 5
text = "abracatabra"
print "{:#>{w}}{}{:%>{w}}".format("", text, "", w=n-1)
This would display:
####abracatabra%%%%
In effect this is printing 3 items, firstly an empty string padded with n-1 # fill characters. Secondly the text string. Lastly another empty string padded with n-1 % fill characters.
Related
I need to print the 2 missing strings KMJD23KN0008393 and KMJD23KN0008394 but what I am receiving is KMJD23KN8393 and KMJD23KN8394 .I need those missing zeros also in our list.
ll = ['KMJD23KN0008391','KMJD23KN0008392','KMJD23KN0008395','KMJD23KN0008396']
missList=[]
for i in ll:
reList=re.findall(r"[^\W\d_]+|\d+", i)
print(reList)
The issue can be decomposed into three parts:
Extracting the trailing number string
Interpreting that substring as the actual number, which should form a consecutive sequence
Re-formatting the missing items based on the surrounding items.
There are multiple assumptions implicit in these items. You need to be aware of these, and ideally make them explicit. In the following, I’ve worked with the following assumptions:
Anything that forms a number at the end of the string is considered. Everything before that is a prefix and is assumed to be identical throughout the series.
The trailing numbers in the series always have the same width.
The items in the list are sorted in ascending order by their trailing number.
The following code implements these assumptions:
all_missing = []
last_num = int(re.search(r'\d+$', ll[-1])[0])
prefix = re.match('.*\D', ll[0])[0]
for item in ll:
num_str = re.search(r'\d+$', item)[0]
num = int(num_str)
num_width = len(num_str)
for missing in range(last_num + 1, num):
all_missing.append(f'{prefix}{missing:0{num_width}}')
last_num = num
print(all_missing)
Some notes here:
To extract the trailing number, a very simple regex is sufficient: \d+$. That is: one or more digits, until the end of the string.
Conversely, to extract the prefix, we search for any sequence of arbitrary characters where the last character is a non-digit. That is: .*\D.
To re-format the missing items, we concatenate the prefix with the missing number, and we pad the missing number with zeros (from left) until it is of the expected width. This is achieved by using Python’s f-strings with the format specifier '0{num_width}'.
I am new to Python (and programming, in general) and hoping to see if someone can help me. I am trying to automate a task that I am currently doing manually but is no longer feasible. I want to find and write all strings between two given strings. For example, if starting and ending strings are XYZ-DF 000010 and XYZ-DF 000014, the desired output should be XYZ-DF 000010; XYZ-DF 000011; XYZ-DF 000012; XYZ-DF 000013; XYZ-DF 000014. The prefix and numbers (and their padding) are not always the same. For example, next starting and ending strings in the list could be ABC_XY00000001 and ABC_XY00000123. The prefix and padding for any pair of starting and ending strings, though, will always be the same.
I think I need to separate the prefix (includes any alphabets, spaces, underscore, hyphen etc.) and numbers, remove padding from the numbers, increment the numbers by 1 from starting number to ending number for every starting and ending strings in a second loop, and then finally get the output by concatenation.
So far this is what I have:
First, I read the 2 columns that contain a list of starting and ending strings in a csv into lists using pandas:
columns = ['Beg', 'End']
data = pd.read_csv('C:/Downloads/test.csv', names=columns, header = None)
begs = data.Beg.tolist()
ends= data.End.tolist()
Next, I loop over "begs" and "ends" using the zip function.
for beg, end in zip(begs,ends):
Inside the loop, I want to iterate over each string in begs and ends (one pair at a time) and perform the following operations on them:
1) Use regex to separate the characters (including alphabets, spaces, underscore, hyphen etc.) from the numbers (including padding) for each of the strings one at a time.
start = re.match(r"([a-z-_ ]+)([0-9]+)", beg, re.I) #Let's assume first starting string in the begs list is "XYZ-DF 000010" from my example above
prefix = start.group(1) #Should yield "XYZ-DF "
start_num = start.group(2) #Should yield "000010"
padding = (len(start_num)) #Yields 6
start_num_stripped = start_num.lstrip("0") #Yields 10
end = re.match(r"([a-z-_ ]+)([0-9]+)", end, re.I) #Let's assume first ending string in the ends list is "XYZ-DF 000014" from my example above
end_num = end.group(2) #Yields 000014
end_num_stripped = end_num.lstrip("0") #Yields 14
2) After these operations, run a nested while loop from start_num_stripped until end_num_stripped
output_string = ""
while start_num_stripped <= end_num_stripped:
output_string = output_string+prefix+start_num_stripped.zfill(padding)+"; "
start_num_stripped += 1
Finally, how do I write the output_string for each pair of starting and ending strings to a csv file that contains 3 columns containing the starting string, ending string, and their output string? An example of an output in csv format is given below (newline after each row is for clarity and not needed in the output).
"Starting String", "Ending String", "Output String"
"ABCD-00001","ABCD-00003","ABCD-00001; ABCD-00002; ABCD-00003"
"XYZ-DF 000010","XYZ-DF 000012","XYZ-DF 000010; XYZ-DF 000011; XYZ-DF 000012"
"BBB_CC0000008","BBB_CC0000014","BBB_CC0000008; BBB_CC0000009; BBB_CC0000010; BBB_CC0000011; BBB_CC0000012; BBB_CC0000013; BBB_CC0000014"
You could find the longest trailing numeric suffix using a regular expression. Then simply iterate numbers from start to end appending them (with leading zeros) to the common prefix:
import re
startString = "XYZ-DF 000010"
endString = "XYZ-DF 000012"
suffixLen = len(re.findall("[0-9]*$",startString)[0])
start = int("1"+startString[-suffixLen:])
end = int("1"+endString[-suffixLen:])
result = [ startString[:-suffixLen]+str(n)[1:] for n in range(start,end+1) ]
csvLine = '"' + '","'.join([ startString,endString,";".join(result) ]) + '"'
print(csvLine) # "XYZ-DF 000010","XYZ-DF 000012","XYZ-DF 000010;XYZ-DF 000011;XYZ-DF 000012"
Note: using int("1" + suffix) causes numbers in the range to always have 1 more digit than the length of the suffix (1xxxxx). This makes it easy to get the leading zeroes by simply dropping the first character after turning them back into strings str(n)[1:]
Note2: I'm not familiar with pandas but I'm pretty sure it has a way to write a csv directly from the result list rather than formatting it manually as I did here in csvLine.
I am trying to match sequences of strings that follow certain rules:
rlg3-22, rlas1-4
pz
xx-0
r1-6
For example, in the first row, I want to match the string up until the "-" character, so that I can perform the following function of expanding the string into (rlg3, rlg4, ..., rlg22).
In the second row, I would leave it as is.
In the third row, I would also leave it as is because there was no number first.
Thank you!
d = 'rlg3-22'
import re
ops = re.findall(r"\d+",d) # r"\d+" searches for digits of variables length
prefix = re.findall(r"\D+", d)[0] # r"\D+" complement set of "\d+"
build the list and add the prefix to the string cast of the integer
[prefix + str(i) for i in list(range(int(ops[0]), int(ops[1]),1))]
['rgl3',
'rgl4',
'rgl5',
'rgl6',
'rgl7',
'rgl8',
'rgl9',
'rgl10',
'rgl11',
'rgl12',
'rgl13',
'rgl14',
'rgl15',
'rgl16',
'rgl17',
'rgl18',
'rgl19',
'rgl20',
'rgl21']
I have a string like so:
string = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255" #(white space sep)
how would you go from it to this format:
string = "27.116.56.0-27.116.59.255,43.230.209.0-43.230.209.255"
**the string will have unknown length , the elements number will allways be even.
I've looked at some close examples and got confused..
what is the best easy way doing that?
# Create a new list containing the ips
str_elems = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255".split()
# Use a format string to build the new representation, where each list element is assigned a spot in the string
# We use the * operator to convert the single list into multiple arguments for the format
new_str = ("{}-{},"*(len(str_elems)/2)).format(*str_elems).rstrip(',')
For a general solution, you can iterate over your split string in chunks of 2.
s = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255".split()
print(",".join(["-".join(s[i:i + 2]) for i in range(0, len(s), 2)]))
#'27.116.56.0-27.116.59.255,43.230.209.0-43.230.209.255'
Join the inner chunks with a "-" and finally join the whole thing with ","
string = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255"
ip_list = string.split(" ") # split the string to a list using space seperator
for i in range(len(ip_list)): # len(ip_list) returns the number of items in the list (4)
# range(4) resolved to 0, 1, 2, 3
if (i % 2 == 0): ip_list[i] += "-" # if i is even number - concatenate hyphen to the current IP string
else: ip_list[i] += "," # otherwize concatenate comma
print("".join(ip_list)[:-1]) # "".join(ip_list) - join the list back to a string
# [:-1] trims the last character of the result (the extra comma)
I'm new to python. I would like to trim a string only if it's last 6 digits are zeros.
I checked the strip() methods, but I don't see one that indicates the n number of characters to trim from right.
sample string:
20121124000000
result string should be:
20121124
s = s[:-6] if s.endswith('000000') else s
or define a function:
def rstrip_chars(s, chars):
return s[:-len(chars)] if s.endswith(chars) else s
s = rstrip_chars(s, '000000')