Get specific words from a string in python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to to extract some information from a data file. The following is the format I have in my data file:
44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)
From this line, I want to extract 256 which is the last number and 4 which is the first number from
(4,0x00002aab 4c000c00)
Could you please recommend some functions which will be useful for my case?

You should use str.split().
What it does is split the string every place there is a space, so you would get a list of strings like so:
n = '44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)'
o = n.split()
print o
Output:
['44', '2.463181s>', '(G)', 'GET_NBI:', '0x00002aaa', 'ecc00e90', '<-', '(4,0x00002aab', '4c000c00)', '(256', 'bytes)']
Then simply get the second-to-last index like o[-2] -> '(256'
Remove the extra parenthesis: '(256'[1:] -> '256', and If you wanna, turn it into an integer. int('256') -> 256

You could also use regular expressions, which in this case might be a bit more clear.
import re
txt = "44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)"
results = re.findall(r"\((\d+)", txt)
# ["4", "256"]

Related

Extraction of data from a delimited string in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a string variable which has some data as shown below:
'From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
The data basically has two columns 'From' and 'To'. How do I extract the entries from the 'To' column in python?
You can use split, and then extract the data from the odd indexes, like so:
data = 'From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
print(data)
data = data.split()
to = [data[i] for i in range(3, len(data), 2)]
print(to)
In python you could split a string at specific chars, in your case \n delimits the row and \t delimits the column
something like this should work:
string='From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
f=[]
t=[]
for row in string.split("\n")[1:]:
fr,to=row.split("\t")
f.append(fr)
t.append(to)
print(f,t)

Efficient way of parsing string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How would you turn a string that looks like this
7.11,8,9:00,9:15,14:30,15:00
into this dictionary entry
{7.11 : [8, (9:00, 9:15), (14:30, 15:00)]}?
Suppose that the number of time pairs (such as 9:00,9:15 and 14:30,15:00 is unknown and you want to have them all as tuple pairs.
First split the string at the commas, then zip cluster starting from the 3rd element and put it into a dictionary:
s = "7.11,8,9:00,9:15,14:30,15:00"
ss = s.split(',')
d = {ss[0]: [ss[1]] + list(zip(*[iter(ss[2:])]*2))}
Output:
{'7.11': ['8', ('9:00', '9:15'), ('14:30', '15:00')]}
If you need to convert it from string to appropiate data types (you'll have to adapt it according to your needs), then after getting the ss list:
time_list = [datetime.datetime.strptime(t,'%H:%M').time() for t in ss[2:]]
d = {float(ss[0]): [int(ss[1])] + list(zip(*[iter(time_list)]*2))}

i cant sum the numbers (python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Write a shell (text-based) program, called sum_num.py, that asks the user for a semicolon (;) separated list of numbers, and calculated the total. See the example below.
a = str(raw_input("Enter semicolon separated list of integers:"))
b = a.split(";")
c = (a[0:])
print("the total is " + sum(c))
PS C:\Users\ssiva\Desktop> python sum_num.py
Enter semicolon separated list of integers: 3;10;4;23;211;3
The total is 254
This code will convert into integers and sum them
a=input()
b=a.split(';')
sum=0
for num in b:
sum+=int(num)
print(sum)

How to read data without specific symbol in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
My dataset looks like following. I am trying to read numbers in "per" column without reading "%" symbol.Being a beginner in python,I was wondering if we can do such in python. Also, if you could provide the explanation that will be great!
State Year per
A 1990 6.10%
A 1989 4.50%
B 1990 3.4%
B 1989 1.25%
Thanks in advance,
In case it is a csv file, this should help (or there might be another way to get a dataframe):
import pandas as pd
data = pd.read_csv("somefile.csv")
data["per"] = data["per"].str.replace("%", "").to_numeric()
Your file type doesn't matter for this and no modules required. It works by taking each row and going to the last word. Then it splits the percentage and removes the percent symbol.
def readFile(filename):
percents = []
with open (filename,"r") as f:
for row in f:#for each line, we remove the first one late
splitRow = row.split()[-1]# spliting the elements by word, we want the last one only
percent = splitRow
percent = percent.split("%")[0]#removing the percent
percents.append(percent)#if you want it as an number instead of a string do percents.append(float(percent))
percents = percents[1:] # removes the header "per"
return percents

Hackerrank Staircase solution not accepted [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
A hackerrank problem demands you to print a staircase out of hashes:
#
##
###
####
#####
######
I've submitted the following code:
n = int(input())
for i in reversed(range(n)):
print(i*' ','#'*(n-i))
It wasn't accepted. Why?
The problem is your print statement.
print(i*' ','#'*(n-i))
If you print multiple strings separated by a comma, you will get the strings delimited by a space character. E.g.
>>> print("foo", "bar")
foo bar
>>> print("foo"+"bar")
foobar
You can combine two strings with the + operator.
Making this small change in your program should solve the problem.
The print() function prints its arguments separated by a space character (" ") by default, giving you extra characters in the output. You need to either print a single argument, or pass sep="":
print(i*' ' + '#'*(n-i))
or
print(i*' ', '#'*(n-i), sep="")

Categories