How to get integer for two characters in python - python

a = "a26lsdm3684"
How can I get an integer with value of 26(a[1] and a[2])? If I write int(a[1) or int (a[2]) it just gives me integer of one character. What should I write when I want integer with value of 26 and store it in variable b?

Slice out the two characters, then convert:
b = int(a[1:3]) # Slices are exclusive on the end index, so you need to go to 3 to get 1 and 2

you can get substrings out of the string and convert that to int, as long as you know the exact indexes
a = "a26lsdm3684"
substring_of_a = a[1:3]
number = int(substring_of_a)
print(number, type(number))

There is more than one way to do it.
Use Slicing, as pointed out by jasonharper and ShadowRanger.
Or use re.findall to find the first stretch of digits.
Or use re.split to split on non-digits and find the 2nd element (the first one is an empty string at the beginning).
import re
a = "a26lsdm3684"
print(int(a[1:3]))
print(int((re.findall(r'\d+', a))[0]))
print(int((re.split(r'\D+', a))[1]))
# 26

A little more sustainable if you want multiple numbers from the same string:
def get_numbers(input_string):
i = 0
buffer = ""
out_list = []
while i < len(input_string):
if input_string[i].isdigit():
buffer = buffer + input_string[i]
else:
if buffer:
out_list.append(int(buffer))
buffer = ""
i = i + 1
if buffer:
out_list.append(int(buffer))
return out_list
a = "a26lsdm3684"
print(get_numbers(a))
output:
[26, 3684]

If you want to convert all the numeric parts in your string, and say put them in a list, you may do something like:
from re import finditer
a = "a26lsdm3684"
s=[int(m.group(0)) for m in finditer(r'\d+', a)] ##[26, 3684]

Related

Python: Replace character RANGE in a string with new string

Given the following string:
mystring = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
The goal is to swap out a character position range with other characters.
For example, swap out characters 20-24 with ABCDE.
The result would look like:
XXXXXXXXXXXXXXXXXXXABCDEXXXXXXXXXXXXXXX
Testing:
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
mystring[20:24] = 'ABCDE'
I get the error: TypeError: 'str' object does not support item assignment
The end goal is a reusable function such as:
def replace_chars(some_string, start_char, end_char, replace_string):
if len(replace_string) == (end_char_pos - start_char_pos + 1):
some_string[start_char:end_char] = replace_string
else:
print "replace string invalid length"
sys.exit(1)
return mystring
new_string = replace_chars('XYZXYZ', 2, 4, 'AAA')
I realize that it's possible to pad out the unchanged range into a new string:
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
mystring = mystring[0:19] + 'ABCDE' + mystring[25:38]
However that will force more calculation and since this will be happening thousands of times against lines in a file. The different lines will be different length and will be different character positions to swap. Doing this seems like it would be a long workaround where I should just be able to insert direct into the character positions in-place.
Appreciate any help, thanks!
strings are immutable (unchangeable). But you can index and join items.
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
mystring = 'ABCDE'.join([mystring[:20],mystring[24:]])
'XXXXXXXXXXXXXXXXXXXXABCDEXXXXXXXXXXXXXX'
Do be careful as the string length "ABCDE" and the number of items you omit between mystring[:20], mystring[24:] need to be the same length.
Strings are immutable in python! You'll have to split the string into three pieces and concatenate them together :)
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
new_str = "ABCDE"
first_piece = mystring[0:20]
third_piece = mystring[24:len(mystring)]
final_string = first_piece + new_str + third_piece
This is not strictly possible in python, but consider using bytearray a similar structure to a string in python, with a key difference being mutability
In [52]: my_stuff = bytearray('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
In [53]: my_stuff = my_stuff[0:19] + "abcd" + my_stuff[25:38]
In [54]: print my_stuff
XXXXXXXXXXXXXXXXXXXabcdXXXXXXXXXXXXX
There are some key things you should know when using a bytearray, you can see some of them here
As much as you think you should be able to assign to individual characters of a string, 'str' object does not support item assignment says you can't.

How to extract string in Python

I want to extract specific string from a string, but it shows error. Why can't I use the find as index to extract string ?
Here is my code
string = 'ABP'
p_index = string.find('P')
s = string[0, p_index]
print(s)
TypeError: string indices must be integers
s = string[0, p_index] isn't a valid syntax in python, you should rather do:
s = string[0:p_index]
Since an omitted first index defaults to zero, this returns the same result:
s = string[:p_index]
I'd recommend reading this page for reference on Python string's slicing and it's syntax in general.
You should change this line:
s = string[0, p_index]
with
s = string[p_index]
You don't need to put anything rather than the index of the letter to get 'P' and you found the index with string.find('P') already.
If you mean substracting the 'P' letter from 'ABP' then use:
new_string = 'ABP'.replace('P','')
I'm pretty sure you slice strings like this s = string[0:2]
string = 'ABP'
p_index = string.index('P')
s = string[p_index]
print(s)
string = 'ABP'
p_index = string.find('P')
s = string[p_index]
print(s)
maybe you can try it like this two

not standard splitting

I have string value like:
a='[-sfdfj aidjf -dugs jfdsif -usda [[s dfdsf sdf]]]'
I want to transform "a" into dictionary: the strings with preceding "-" character should be keys and what goes after the space should be values of the key preceding it.
If we are working with "a", then what I want is the resulting dictionary like:
dict_a={'-sfdfj': 'aidjf', '-dugs': 'jfdsif', '-usda': '[[s dfdsf sdf]]'}
This would be simple if not the last value('[[s dfdsf sdf]]'), it contains the spaces. Otherwise I would just strip the external brackets and split the "a", then convert the resulting list into dict_a, but alas the reality is not on my side.
Even if I get the list like:
list_a=['-sfdfj', 'aidjf', '-dugs', 'jfdsif', '-usda', '[[s dfdsf sdf]']
this would be enough.
Any help will be appreciated.
You can split the string by '-' and then add the '-' back.
a = '[-sfdfj aidjf -dugs jfdsif -usda [[s dfdsf sdf]]]'
a = a[1:-1] # get ride of the start and end []
sections = a.split('-')
dict_a = {}
for s in sections:
s = s.strip()
if len(s) == 0:
continue
key_value = s.split(' ') # split key value by space
key = '-' + key_value[0] # the first element is key
value = ' '.join(key_value[1:]) # the lefe are value
dict_a[key] = value
I can tell you a way to go about it.
Strip the quotes and the outer brackets. Then split the string using spaces. Iterate over the list obtained and check for any opening brackets. Keep a count of the number of opening brackets, join all the list items as a string with spaces between each such item until you encounter an equal number of closing brackets. The remaining items remain as is. You could try implementing it. If you face any issues, I'll help you with the code.
#chong's answer is a neater way to go about it.
Using a regular expression:
>>> import re
>>> dict(re.findall('(-\S+) ([^-]+)', a[:-1].replace(' -', '-')))
{'-sfdfj': 'aidjf', '-dugs': 'jfdsif', '-usda': '[[s dfdsf sdf]]'}
Using #ChongTang's idea:
>>> dict(('-' + b).strip().split(maxsplit=1) for b in a[1:-1].split('-') if b)
{'-sfdfj': 'aidjf', '-dugs': 'jfdsif', '-usda': '[[s dfdsf sdf]]'}
You can try this:
import re
a='[-sfdfj aidjf -dugs jfdsif -usda [[s dfdsf sdf]]]'
pattern_key=re.compile(r'(?P<key>-\S+)\s+')
pattern_val=re.compile(r' (?P<val>[^-].*?)( -|\Z)')
d={}
matches=pattern_key.finditer(a)
matches1=pattern_val.finditer(a)
for m,n in zip(matches, matches1):
d[m.group('key')]= n.group('val')
print d

Split string by hyphen

I have a strings in the format of feet'-inches" (i.e. 18'-6") and I want to split it so that the values of the feet and inches are separated.
I have tried:
re.split(r'\s|-', `18'-6`)
but it still returns 18'-6.
Desired output: [18,6] or similar
Thanks!
Just split normally replacing the ':
s="18'-6"
a, b = s.replace("'","").split("-")
print(a,b)
If you have both " and ' one must be escaped so just split and slice up to the second last character:
s = "18'-6\""
a, b = s.split("-")
print(a[:-1], b[:-1])
18 6
You can use
import re
p = re.compile(ur'[-\'"]')
test_str = u"18'-6\""
print filter(None,re.split(p, test_str))
Output:
[u'18', u'6']
Ideone demo
A list comprehension will do the trick:
In [13]: [int(i[:-1]) for i in re.split(r'\s|-', "18'-6\"")]
Out[13]: [18, 6]
This assumes that your string is of the format feet(int)'-inches(int)", and you are trying to get the actual ints back, not just numbers in string format.
The built-in split method can take an argument that will cause it to split at the specified point.
"18'-16\"".replace("'", "").replace("\"", "").split("-")
A one-liner. :)

how to split brackets using python abcd[00451.00]

I have tried below code to split but I am unable to split
import re
s = "abcd[00451.00]"
print str(s).strip('[]')
I need output as only number or decimal format 00451.00 this value but I am able to get output as abcd[00451.00
If you know for sure that there will be one opening and closing brackets you can do
s = "abcd[00451.00]"
print s[s.index("[") + 1:s.rindex("]")]
# 00451.00
str.index is used to get the first index of the element [ in the string, where as str.rindex is used to get the last index of the element in ]. Based on those indexes, the string is sliced.
If you want to convert that to a floating point number, then you can use float function, like this
print float(s[s.index("[") + 1:s.rindex("]")])
# 451.0
You should use re.search:
import re
s = "abcd[00451.00]"
>>> print re.search(r'\[([^\]]+)\]', s).group(1)
00451.00
You can first split on the '[' and then strip the resulting list of any ']' chars:
[p.strip(']') for p in s.split('[')]

Categories