Home > Mobile >  Using .split(), I want save a string into 2 or 3 separate variables
Using .split(), I want save a string into 2 or 3 separate variables

Time:01-30

Lets assume I have this in the line line=vaddpd %ymm8,%ymm7,%ymm6

what I want is to get a=%ymm8, b=%ymm7, c=%ymm6

or lets say we have in the line line=cmp %rsi,%rbx

i want to get a=%rsi, b=%rbx

x=line.split(",")

CodePudding user response:

import re

line = "vaddpd %ymm8,%ymm7,%ymm6"
splt_line = re.split(r"[ ,]", line)

After this, you can assign elements of splt_line to any variable you want.

CodePudding user response:

Start with using split on the space:

line="vaddpd %ymm8,%ymm7,%ymm6"
split_list = line.split(" ")[1].split(",")

Then you have a list of your desired variables. You can assign them however you like, for instance a dictionary:

import string
keys = list(string.ascii_lowercase)
my_dict = dict(zip(keys, split_list))
  •  Tags:  
  • Related