Extraction of data from a delimited 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 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)

Related

How could I manipulate data in a python list to modify and store? [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 want to manipulate some data stored in a variable list and store it to a new variable in python :
oldvar =[]
newvar = []
values in oldvar in the format:
print(oldvar)
output:
[docker.xxx.com/service-1:2021.06.23-061151,docker.xxx.com/service-2:2021.06.28-052524,docker.xxx.com/service-3:2021.07.13-055424]
values in newvar expected to be in the format:
print(newvar)
output:
[service-1,service-2,service-3]
How can I read the data in the "oldvar" to modify it and store it as I mentioned in the output of "newvar"
You can use str.split + list-comprehension:
oldvar = [
"docker.xxx.com/service-1:2021.06.23-061151",
"docker.xxx.com/service-2:2021.06.28-052524",
"docker.xxx.com/service-3:2021.07.13-055424",
]
newvar = [v.split("/")[-1].split(":")[0] for v in oldvar]
print(newvar)
Prints:
['service-1', 'service-2', 'service-3']

Return part of string between specific character pair, in string with multiple character pairs [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
Text file or string:
SomeText1/SomeText2/SomeText3/SomeText4/SomeText5
#What I am looking for:
split_func(3, "/")
>>> SomeText3
Try:
s = "SomeText1/SomeText2/SomeText3/SomeText4/SomeText5"
# s.split("/") returns a list of strings, split at the "/"
# I.e. ["SomeText1", "SomeText2", "SomeText3", "SomeText4", "SomeText5"]
# Then take the second element (remembering that the count starts at 0
result = s.split("/")[2]

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))}

create more lists from one big list [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 2 years ago.
Improve this question
I have the following list which was created from a text file that I already have.
In the end I have a list that contains all the values that I need from the text, and now I am trying to have a more than one small lists from the list array where every small list start with switch and end where the value is empty.
with open("read.txt") as f:
for line in f:
if line.startswith('switch '):
array.append(line)
for line in f: # Continue iterating f for additional lines to keep
if not line.rstrip():
break # We hit an empty line, return to looking for switch
array.append(line)
i = 0
while i+4 <= len(my_list):
print(my_list[i+1:i+4])
i+=4
#['v1', 'v2', 'v3']
#['m1', 'm2', 'm3']

Get specific words from a 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 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"]

Categories