How to store number from string in python - python

i have string like this:
string = "The stock item "28031 (111111: Test product)" was added successfully."
I need store from string the first 5 numbers ( for example "28031" ) and save them to another string.
It's because i am selenium tester and every time i am create new stock item he has different first 5 numbers.
Thank you for your help
Filip

m = re.search("\d+", string)
print m.group(0)
prints 28031
It just selects the first group of digits, regardless of the length (2803 would be selected, too)

Firstly I am assuming all these strings have exactly the same format. If so the simplest way to get your stock item number is:
stocknumber = string.split()[3][1:]

After sehe answer I leave mine edited just to show how to match 5 digits
import re
re.search('\d{5}', string).group(0)

EDIT : neurino solution is the smartest!! use it
EDIT : sehe solution is smart and perfect you can add this line to get only the first 5 numbers:
print m.group(0)[0:5]
using [0:5] means to take string elements from 0 to 5 (first 5 elements)
use the str.isdigit built-in function
string = "The stock item 28031 "
Digitstring=''
for i in string:
if i.isdigit():
Digitstring+=i
print Digitstring
Output:
28031
you can count to first x numbers you need and then stop.

Related

How to grab a specified number of characters after a part of a specified string?

Let's say I have a string defined like this:
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
The goal is to grab the 11 characters (these for example '345jk3n45jk') after a part of the string (this part for example 'randomstring') using a specified search term and the specified number of characters to grab after that search term.
I tried doing something like this:
string2 = substring(string1,'randomstring', 11)
I appreciate any help you guys have to offer!
string2 = string1[string1.find("randomstring")+len("randomstring"):string1.find("randomstring")+len("randomstring")+11]
In one line, using split, and supposing that your randomstring is unique in your string, which seems to be the case as you worded out the question :
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
randomstring = 'randomstring'
nb_char_to_take = 11
# split using randomstring as splitter, take part of the string after, i.e the second part of the array, and then the 11 first character
result = string1.split(randomstring)[1][:nb_char_to_take]
You can use a simple regular expression like this
import re
s = "23h4b245hjrandomstring345jk3n45jkotherrandomstring"
result = re.findall("randomstring(.{11})", s)[0]
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
string2 = string1[10:22]
print(string2)
randomstring
You could use that. Its called string slicing, you basically count the position of the letters and then the first number before the colon is your starting point the second is your ending point when you enter those position numbers you should get whatever is in-between those position, the last is for a different function I highly suggest you search string slicing on YouTube as my explanation wouldn't really help you, and also search up * Find string method* those should hep you get the idea behind those functions. Sorry couldn't be of much help hope the videos help.

How do I split a String Array into 2 serperately parts

How do I split a string Array in some separately parts?
that I can print separately
I even don't know how to start
can anyone help to give me push to the right direction
thank you
Python stores a string as list only. Lets say you have :
s = 'abcd'
you can access separate the list using concept of substring
s[1:2]
>> b
s[:3]
>>abc
Similarly, For
s = ['hello my name is anton','I am 9999 years old']
''.join(s[1])
''.join(s[1:2])
You can take a look at List Slicing
string='hai, how is your day going? is it good or bad ?'
if you want to split a string like this with ,/./? then you can use
string.split('?')
it will split using the '?' symbol
Or You can use the slice methord
string[0:4]
here 0 is the starting character index and 4 is the ending character index
string[start:end]

Python - Finding all numeric values in a string, then storing each numeric in a list uniquely

I would like to be able to grab any and all numeric values from a string if found. Then store them in a list individually.
Currently able to identify all numeric values, but not able to figure out how to store them individually.
phones = list()
comment = "Sues phone numbers are P#3774794773 and P#6047947730."
words = comment.split()
for word in words:
word = word.rstrip()
nums = re.findall(r'\d{10,10}',word)
if nums not in phones:
phones.append(nums)
print(phones)
I would like to get those two values to be stored as such.... 3774794773,6047947730. Instead of a list within a list.
End goal output (print) each value separately.
Current Print: [ [], ['3774794773'], ['6047947730'] ]
Needed Print: 3774794773, 6047947730
Thanks in advance.
You're doing a double job with the regex (split is also basically regex based) just do the whole thing with a 10 digit number matching regex, like so:
comment = "Sues phone numbers are P#3774794773 and P#6047947730."
nums = re.findall(r'\d{10,10}', comment)
print(nums)
If you want the numbers also to be exact (not to match longer sequences) you can do the following:
comment = "Sues phone numbers are P#3774794773 123145125125215 and P#6047947730."
nums = re.findall(r'\b\d{10,10}\b', comment)
print(nums)
(\b is an interesting regex symbol which doesn't really match a part of the string but rather matches "the space between characters" in the string)
both result in:
['3774794773', '6047947730']
Save your comment variable in a file and then use this code to separate them into variables
with open("CS.txt", "r") as f:
number1,number2 = f.read().split(" ")
print(number1)
print(number2)

How to attach a string to an integer in Python 3

I've got a two letter word that I'd like to attach to a double digit number. The word is an integer and the number is a string.
Say the name of the number is "number" and the name of the word is "word".
How would you make it print both of them together without spaces. When I try it right now it still has a space between them regardless of what I try.
Thanks !
'{}{}'.format(word, number)
For example,
In [19]: word='XY'
In [20]: number=123
In [21]: print('{}{}'.format(word, number))
XY123
The print function has a sep parameter that controls spacing between items:
print(number, word, sep="")
If you need a string, rather than printing, than unutbu's answer with string formatting is better, but this may get you to your desired results with fewer steps.
In python 3 the preferred way to construct strings is by using format
To print out a word and a number joined together you would use:
print("{}{}".format(word, number))

Remove -#### in zipcodes

How do I remove the +4 from zipcodes, in python?
I've got data like
85001
52804-3233
Winston-Salem
And I want that to become
85001
52804
Winston-Salem
>>> zip = '52804-3233'
>>> zip[:5]
'52804'
...and of course when you parse your lines from the original data you should insert some kind of rule to distinguish between zipcode to fix and other strings, but I don't know how your data looks like, so I can't help much (you could check if they are only digits and the '-' symbol, maybe?).
>>> import re
>>> s = "52804-3233"
>>> # regex to remove a dash and 4 digits after the dash after 5 digits:
>>> re.sub('(\d{5})-\d{4}', '\\1', s)
'52804'
The \\1 is a so called back reference and gets replaced by the first group, which would be the 5 digit zipcode in this case.
You could try something like this:
for input in inputs:
if input[:5].isnumeric():
input = input[:5]
# Takes the first 5 characters from the string
Just take away the first 5 characters of anything that is numbers in the first 5 positions.
re.sub('-\d{4}$', '', zipcode)
This grabs all items of the format 00000-0000 with a space or other word boundary before and after the number and replaces it with the first five digits. The other regex's posted will match some other number formats that you might not want.
re.sub('\b(\d{5})-\d{4}\b', '\\1', zipcode)
Or without regex:
output = [line[:5] if line[:5].isnumeric() and line[6:].isnumeric() else line for line in text if line]

Categories

Resources