Extract number from the string
Question:
I’m solving an assignment in python and found it difficult to extract a number from a string.
It is to read data from a file and extract numbers from strings and perform operations on the value.
Tesla Model 3 LR 4WD:560
Tesla Model Y LR 2WD:540
This is a string and I have to extract only the value after the colon.
Answers:
Split the string at n
characters. Then for every line, split it at :
and append the number to a list:
string = "Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540"
lines = string.split("n")
nums = []
for line in lines:
nums.append(int(line.split(":")[-1]))
Split the string at n characters. Then for every line, split it at : and append the number to a list:
string = "Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540"
lines = string.split("n")
nums = []
for line in lines:
nums.append(int(line.split(":")[-1]))
I assume that you get data as string. Also, you need value after the colon.
Then you can use split function from string.
data = "Tesla Model 3 LR 4WD:560"
value = data.split(":")[-1]
If you can use a regex:
string = 'Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540'
import re
out = re.findall('(?<=:)d+', string)
Output: ['560', '540']
If you want integers:
out = [int(x.group()) for x in re.finditer('(?<=:)d+', string)]
Output: [560, 540]
This snippet should read strings from file, and extract the numbers from the strings into a list.
def findNumber(text):
# Returns the number after the colon as integer.
return int(text.split(':')[-1])
# Open the file containing the lines/strings
with open('file.txt') as file:
lines = file.readlines()
# extract the number fr
data = list(map(findNumber, lines))
print(data)
You can also use list comprehension:
text = 'Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540'
num = [int(n) for i in text.split('n') for n in i.split(':') if n.isdigit()]
print(num)
# [560, 540]
I’m solving an assignment in python and found it difficult to extract a number from a string.
It is to read data from a file and extract numbers from strings and perform operations on the value.
Tesla Model 3 LR 4WD:560
Tesla Model Y LR 2WD:540
This is a string and I have to extract only the value after the colon.
Split the string at n
characters. Then for every line, split it at :
and append the number to a list:
string = "Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540"
lines = string.split("n")
nums = []
for line in lines:
nums.append(int(line.split(":")[-1]))
Split the string at n characters. Then for every line, split it at : and append the number to a list:
string = "Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540"
lines = string.split("n")
nums = []
for line in lines:
nums.append(int(line.split(":")[-1]))
I assume that you get data as string. Also, you need value after the colon.
Then you can use split function from string.
data = "Tesla Model 3 LR 4WD:560"
value = data.split(":")[-1]
If you can use a regex:
string = 'Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540'
import re
out = re.findall('(?<=:)d+', string)
Output: ['560', '540']
If you want integers:
out = [int(x.group()) for x in re.finditer('(?<=:)d+', string)]
Output: [560, 540]
This snippet should read strings from file, and extract the numbers from the strings into a list.
def findNumber(text):
# Returns the number after the colon as integer.
return int(text.split(':')[-1])
# Open the file containing the lines/strings
with open('file.txt') as file:
lines = file.readlines()
# extract the number fr
data = list(map(findNumber, lines))
print(data)
You can also use list comprehension:
text = 'Tesla Model 3 LR 4WD:560nTesla Model Y LR 2WD:540'
num = [int(n) for i in text.split('n') for n in i.split(':') if n.isdigit()]
print(num)
# [560, 540]