How to split each individual value between two string in Python
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
How can I split each value which is between (;
and ~
)?
The result will be like CREATEDBY,CREATEDBYNAME,CREATEDBYYOMINAME,...
I have tried the below, but it's giving the first occurrence.
variable[variable.find(";")+1:myString.find("~")]
How do I get the list of strings by using the split?
python python-2.7
add a comment |
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
How can I split each value which is between (;
and ~
)?
The result will be like CREATEDBY,CREATEDBYNAME,CREATEDBYYOMINAME,...
I have tried the below, but it's giving the first occurrence.
variable[variable.find(";")+1:myString.find("~")]
How do I get the list of strings by using the split?
python python-2.7
use split function . mystring.split(';') then mystirng.split('~')
– Hassan ALi
Dec 18 '18 at 8:37
3
Possible duplicate of How to split a string into a list? Note that the second answer indicates how to specify the delimiter.
– jpmc26
Dec 18 '18 at 18:14
add a comment |
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
How can I split each value which is between (;
and ~
)?
The result will be like CREATEDBY,CREATEDBYNAME,CREATEDBYYOMINAME,...
I have tried the below, but it's giving the first occurrence.
variable[variable.find(";")+1:myString.find("~")]
How do I get the list of strings by using the split?
python python-2.7
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
How can I split each value which is between (;
and ~
)?
The result will be like CREATEDBY,CREATEDBYNAME,CREATEDBYYOMINAME,...
I have tried the below, but it's giving the first occurrence.
variable[variable.find(";")+1:myString.find("~")]
How do I get the list of strings by using the split?
python python-2.7
python python-2.7
edited Dec 18 '18 at 20:05
Peter Mortensen
13.5k1983111
13.5k1983111
asked Dec 18 '18 at 8:32
Vicky
4841926
4841926
use split function . mystring.split(';') then mystirng.split('~')
– Hassan ALi
Dec 18 '18 at 8:37
3
Possible duplicate of How to split a string into a list? Note that the second answer indicates how to specify the delimiter.
– jpmc26
Dec 18 '18 at 18:14
add a comment |
use split function . mystring.split(';') then mystirng.split('~')
– Hassan ALi
Dec 18 '18 at 8:37
3
Possible duplicate of How to split a string into a list? Note that the second answer indicates how to specify the delimiter.
– jpmc26
Dec 18 '18 at 18:14
use split function . mystring.split(';') then mystirng.split('~')
– Hassan ALi
Dec 18 '18 at 8:37
use split function . mystring.split(';') then mystirng.split('~')
– Hassan ALi
Dec 18 '18 at 8:37
3
3
Possible duplicate of How to split a string into a list? Note that the second answer indicates how to specify the delimiter.
– jpmc26
Dec 18 '18 at 18:14
Possible duplicate of How to split a string into a list? Note that the second answer indicates how to specify the delimiter.
– jpmc26
Dec 18 '18 at 18:14
add a comment |
6 Answers
6
active
oldest
votes
Using str.split
Ex:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
for i in variable.strip(";").split(";"):
print(i.split("~", 1)[0])
#or
print([i.split("~", 1)[0] for i in variable.strip(";").split(";")])
Output:
CREATEDBY
CREATEDBYNAME
CREATEDBYYOMINAME
CREATEDON
CREATEDONUTC
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
add a comment |
We can try using re.findall
with the pattern ;(w+)(?=~)
:
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = re.findall(r';(w+)~', variable)
print(result)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
It's rejecting if the COLUMN name has '_' (i.e CREATED_BY)
– Vicky
Dec 18 '18 at 9:11
2
@Vicky Then usew+
, which includes underscores, to match your words.
– Tim Biegeleisen
Dec 18 '18 at 9:21
@Vicky: good observation. That's why I hate Regex. You never know what they are really doing and what their intension was. It's very easy to get them wrong accidentally.
– Thomas Weller
Dec 18 '18 at 10:26
1
@Thomas I'm not sure that's a reason to hate Regex.[A-Z]
should be self-explanatory that it isn't going to match underscores, and the question could have been clearer that underscores should be matched.
– grg
Dec 18 '18 at 17:10
1
@JaredSmith: trivial or not, I can't judge. The spec is not so bad regarding the underscore: between;
and~
, it did not say that there should be a word (w
). So a[^~]
would be closer to the spec. Next, I wonder why he says the pattern should be;(w+)(?=~)
but then uses;(w+)~
in the code.
– Thomas Weller
Dec 18 '18 at 18:51
|
show 1 more comment
You can split()
the string and then find()
the first ~
for each one:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = [item[:item.find('~')] for item in variable.split(';')]
print(result)
add a comment |
Use regular expression with lookahead and lookbehind:
>>> import re
>>> re.findall(r'(?<=;).*?(?=~)', variable)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
Posted the same answer 5 minutes ago. You don't even need lookarounds if you place the name in a capture group.
– Tim Biegeleisen
Dec 18 '18 at 8:46
3
The two answers are fairly similar, yes, but I wouldn't call them the same.
– Martin Frodl
Dec 18 '18 at 8:54
add a comment |
import re
s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print result.group(1)
How will this give more than 1 occurrence as needed by OP? And how will this fix the additional~
s? Try your approach with OPs original data and you'll find out that this is nonsense:s = 'a;iwantthis~oops~' result = re.search(';(.*)~', s)
– Thomas Weller
Dec 18 '18 at 18:58
add a comment |
import re
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
pattern = re.compile (";(.+?)~")
matches = re.findall ( pattern, variable )
print matches
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53829045%2fhow-to-split-each-individual-value-between-two-string-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using str.split
Ex:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
for i in variable.strip(";").split(";"):
print(i.split("~", 1)[0])
#or
print([i.split("~", 1)[0] for i in variable.strip(";").split(";")])
Output:
CREATEDBY
CREATEDBYNAME
CREATEDBYYOMINAME
CREATEDON
CREATEDONUTC
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
add a comment |
Using str.split
Ex:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
for i in variable.strip(";").split(";"):
print(i.split("~", 1)[0])
#or
print([i.split("~", 1)[0] for i in variable.strip(";").split(";")])
Output:
CREATEDBY
CREATEDBYNAME
CREATEDBYYOMINAME
CREATEDON
CREATEDONUTC
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
add a comment |
Using str.split
Ex:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
for i in variable.strip(";").split(";"):
print(i.split("~", 1)[0])
#or
print([i.split("~", 1)[0] for i in variable.strip(";").split(";")])
Output:
CREATEDBY
CREATEDBYNAME
CREATEDBYYOMINAME
CREATEDON
CREATEDONUTC
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
Using str.split
Ex:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
for i in variable.strip(";").split(";"):
print(i.split("~", 1)[0])
#or
print([i.split("~", 1)[0] for i in variable.strip(";").split(";")])
Output:
CREATEDBY
CREATEDBYNAME
CREATEDBYYOMINAME
CREATEDON
CREATEDONUTC
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
answered Dec 18 '18 at 8:39
Rakesh
37.3k113870
37.3k113870
add a comment |
add a comment |
We can try using re.findall
with the pattern ;(w+)(?=~)
:
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = re.findall(r';(w+)~', variable)
print(result)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
It's rejecting if the COLUMN name has '_' (i.e CREATED_BY)
– Vicky
Dec 18 '18 at 9:11
2
@Vicky Then usew+
, which includes underscores, to match your words.
– Tim Biegeleisen
Dec 18 '18 at 9:21
@Vicky: good observation. That's why I hate Regex. You never know what they are really doing and what their intension was. It's very easy to get them wrong accidentally.
– Thomas Weller
Dec 18 '18 at 10:26
1
@Thomas I'm not sure that's a reason to hate Regex.[A-Z]
should be self-explanatory that it isn't going to match underscores, and the question could have been clearer that underscores should be matched.
– grg
Dec 18 '18 at 17:10
1
@JaredSmith: trivial or not, I can't judge. The spec is not so bad regarding the underscore: between;
and~
, it did not say that there should be a word (w
). So a[^~]
would be closer to the spec. Next, I wonder why he says the pattern should be;(w+)(?=~)
but then uses;(w+)~
in the code.
– Thomas Weller
Dec 18 '18 at 18:51
|
show 1 more comment
We can try using re.findall
with the pattern ;(w+)(?=~)
:
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = re.findall(r';(w+)~', variable)
print(result)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
It's rejecting if the COLUMN name has '_' (i.e CREATED_BY)
– Vicky
Dec 18 '18 at 9:11
2
@Vicky Then usew+
, which includes underscores, to match your words.
– Tim Biegeleisen
Dec 18 '18 at 9:21
@Vicky: good observation. That's why I hate Regex. You never know what they are really doing and what their intension was. It's very easy to get them wrong accidentally.
– Thomas Weller
Dec 18 '18 at 10:26
1
@Thomas I'm not sure that's a reason to hate Regex.[A-Z]
should be self-explanatory that it isn't going to match underscores, and the question could have been clearer that underscores should be matched.
– grg
Dec 18 '18 at 17:10
1
@JaredSmith: trivial or not, I can't judge. The spec is not so bad regarding the underscore: between;
and~
, it did not say that there should be a word (w
). So a[^~]
would be closer to the spec. Next, I wonder why he says the pattern should be;(w+)(?=~)
but then uses;(w+)~
in the code.
– Thomas Weller
Dec 18 '18 at 18:51
|
show 1 more comment
We can try using re.findall
with the pattern ;(w+)(?=~)
:
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = re.findall(r';(w+)~', variable)
print(result)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
We can try using re.findall
with the pattern ;(w+)(?=~)
:
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = re.findall(r';(w+)~', variable)
print(result)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
edited Dec 18 '18 at 9:33
answered Dec 18 '18 at 8:38
Tim Biegeleisen
217k1386139
217k1386139
It's rejecting if the COLUMN name has '_' (i.e CREATED_BY)
– Vicky
Dec 18 '18 at 9:11
2
@Vicky Then usew+
, which includes underscores, to match your words.
– Tim Biegeleisen
Dec 18 '18 at 9:21
@Vicky: good observation. That's why I hate Regex. You never know what they are really doing and what their intension was. It's very easy to get them wrong accidentally.
– Thomas Weller
Dec 18 '18 at 10:26
1
@Thomas I'm not sure that's a reason to hate Regex.[A-Z]
should be self-explanatory that it isn't going to match underscores, and the question could have been clearer that underscores should be matched.
– grg
Dec 18 '18 at 17:10
1
@JaredSmith: trivial or not, I can't judge. The spec is not so bad regarding the underscore: between;
and~
, it did not say that there should be a word (w
). So a[^~]
would be closer to the spec. Next, I wonder why he says the pattern should be;(w+)(?=~)
but then uses;(w+)~
in the code.
– Thomas Weller
Dec 18 '18 at 18:51
|
show 1 more comment
It's rejecting if the COLUMN name has '_' (i.e CREATED_BY)
– Vicky
Dec 18 '18 at 9:11
2
@Vicky Then usew+
, which includes underscores, to match your words.
– Tim Biegeleisen
Dec 18 '18 at 9:21
@Vicky: good observation. That's why I hate Regex. You never know what they are really doing and what their intension was. It's very easy to get them wrong accidentally.
– Thomas Weller
Dec 18 '18 at 10:26
1
@Thomas I'm not sure that's a reason to hate Regex.[A-Z]
should be self-explanatory that it isn't going to match underscores, and the question could have been clearer that underscores should be matched.
– grg
Dec 18 '18 at 17:10
1
@JaredSmith: trivial or not, I can't judge. The spec is not so bad regarding the underscore: between;
and~
, it did not say that there should be a word (w
). So a[^~]
would be closer to the spec. Next, I wonder why he says the pattern should be;(w+)(?=~)
but then uses;(w+)~
in the code.
– Thomas Weller
Dec 18 '18 at 18:51
It's rejecting if the COLUMN name has '_' (i.e CREATED_BY)
– Vicky
Dec 18 '18 at 9:11
It's rejecting if the COLUMN name has '_' (i.e CREATED_BY)
– Vicky
Dec 18 '18 at 9:11
2
2
@Vicky Then use
w+
, which includes underscores, to match your words.– Tim Biegeleisen
Dec 18 '18 at 9:21
@Vicky Then use
w+
, which includes underscores, to match your words.– Tim Biegeleisen
Dec 18 '18 at 9:21
@Vicky: good observation. That's why I hate Regex. You never know what they are really doing and what their intension was. It's very easy to get them wrong accidentally.
– Thomas Weller
Dec 18 '18 at 10:26
@Vicky: good observation. That's why I hate Regex. You never know what they are really doing and what their intension was. It's very easy to get them wrong accidentally.
– Thomas Weller
Dec 18 '18 at 10:26
1
1
@Thomas I'm not sure that's a reason to hate Regex.
[A-Z]
should be self-explanatory that it isn't going to match underscores, and the question could have been clearer that underscores should be matched.– grg
Dec 18 '18 at 17:10
@Thomas I'm not sure that's a reason to hate Regex.
[A-Z]
should be self-explanatory that it isn't going to match underscores, and the question could have been clearer that underscores should be matched.– grg
Dec 18 '18 at 17:10
1
1
@JaredSmith: trivial or not, I can't judge. The spec is not so bad regarding the underscore: between
;
and ~
, it did not say that there should be a word (w
). So a [^~]
would be closer to the spec. Next, I wonder why he says the pattern should be ;(w+)(?=~)
but then uses ;(w+)~
in the code.– Thomas Weller
Dec 18 '18 at 18:51
@JaredSmith: trivial or not, I can't judge. The spec is not so bad regarding the underscore: between
;
and ~
, it did not say that there should be a word (w
). So a [^~]
would be closer to the spec. Next, I wonder why he says the pattern should be ;(w+)(?=~)
but then uses ;(w+)~
in the code.– Thomas Weller
Dec 18 '18 at 18:51
|
show 1 more comment
You can split()
the string and then find()
the first ~
for each one:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = [item[:item.find('~')] for item in variable.split(';')]
print(result)
add a comment |
You can split()
the string and then find()
the first ~
for each one:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = [item[:item.find('~')] for item in variable.split(';')]
print(result)
add a comment |
You can split()
the string and then find()
the first ~
for each one:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = [item[:item.find('~')] for item in variable.split(';')]
print(result)
You can split()
the string and then find()
the first ~
for each one:
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
result = [item[:item.find('~')] for item in variable.split(';')]
print(result)
answered Dec 18 '18 at 8:39
fixatd
617411
617411
add a comment |
add a comment |
Use regular expression with lookahead and lookbehind:
>>> import re
>>> re.findall(r'(?<=;).*?(?=~)', variable)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
Posted the same answer 5 minutes ago. You don't even need lookarounds if you place the name in a capture group.
– Tim Biegeleisen
Dec 18 '18 at 8:46
3
The two answers are fairly similar, yes, but I wouldn't call them the same.
– Martin Frodl
Dec 18 '18 at 8:54
add a comment |
Use regular expression with lookahead and lookbehind:
>>> import re
>>> re.findall(r'(?<=;).*?(?=~)', variable)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
Posted the same answer 5 minutes ago. You don't even need lookarounds if you place the name in a capture group.
– Tim Biegeleisen
Dec 18 '18 at 8:46
3
The two answers are fairly similar, yes, but I wouldn't call them the same.
– Martin Frodl
Dec 18 '18 at 8:54
add a comment |
Use regular expression with lookahead and lookbehind:
>>> import re
>>> re.findall(r'(?<=;).*?(?=~)', variable)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
Use regular expression with lookahead and lookbehind:
>>> import re
>>> re.findall(r'(?<=;).*?(?=~)', variable)
['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC']
answered Dec 18 '18 at 8:44
Martin Frodl
638310
638310
Posted the same answer 5 minutes ago. You don't even need lookarounds if you place the name in a capture group.
– Tim Biegeleisen
Dec 18 '18 at 8:46
3
The two answers are fairly similar, yes, but I wouldn't call them the same.
– Martin Frodl
Dec 18 '18 at 8:54
add a comment |
Posted the same answer 5 minutes ago. You don't even need lookarounds if you place the name in a capture group.
– Tim Biegeleisen
Dec 18 '18 at 8:46
3
The two answers are fairly similar, yes, but I wouldn't call them the same.
– Martin Frodl
Dec 18 '18 at 8:54
Posted the same answer 5 minutes ago. You don't even need lookarounds if you place the name in a capture group.
– Tim Biegeleisen
Dec 18 '18 at 8:46
Posted the same answer 5 minutes ago. You don't even need lookarounds if you place the name in a capture group.
– Tim Biegeleisen
Dec 18 '18 at 8:46
3
3
The two answers are fairly similar, yes, but I wouldn't call them the same.
– Martin Frodl
Dec 18 '18 at 8:54
The two answers are fairly similar, yes, but I wouldn't call them the same.
– Martin Frodl
Dec 18 '18 at 8:54
add a comment |
import re
s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print result.group(1)
How will this give more than 1 occurrence as needed by OP? And how will this fix the additional~
s? Try your approach with OPs original data and you'll find out that this is nonsense:s = 'a;iwantthis~oops~' result = re.search(';(.*)~', s)
– Thomas Weller
Dec 18 '18 at 18:58
add a comment |
import re
s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print result.group(1)
How will this give more than 1 occurrence as needed by OP? And how will this fix the additional~
s? Try your approach with OPs original data and you'll find out that this is nonsense:s = 'a;iwantthis~oops~' result = re.search(';(.*)~', s)
– Thomas Weller
Dec 18 '18 at 18:58
add a comment |
import re
s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print result.group(1)
import re
s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print result.group(1)
answered Dec 18 '18 at 8:38
Test Project
455
455
How will this give more than 1 occurrence as needed by OP? And how will this fix the additional~
s? Try your approach with OPs original data and you'll find out that this is nonsense:s = 'a;iwantthis~oops~' result = re.search(';(.*)~', s)
– Thomas Weller
Dec 18 '18 at 18:58
add a comment |
How will this give more than 1 occurrence as needed by OP? And how will this fix the additional~
s? Try your approach with OPs original data and you'll find out that this is nonsense:s = 'a;iwantthis~oops~' result = re.search(';(.*)~', s)
– Thomas Weller
Dec 18 '18 at 18:58
How will this give more than 1 occurrence as needed by OP? And how will this fix the additional
~
s? Try your approach with OPs original data and you'll find out that this is nonsense: s = 'a;iwantthis~oops~' result = re.search(';(.*)~', s)
– Thomas Weller
Dec 18 '18 at 18:58
How will this give more than 1 occurrence as needed by OP? And how will this fix the additional
~
s? Try your approach with OPs original data and you'll find out that this is nonsense: s = 'a;iwantthis~oops~' result = re.search(';(.*)~', s)
– Thomas Weller
Dec 18 '18 at 18:58
add a comment |
import re
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
pattern = re.compile (";(.+?)~")
matches = re.findall ( pattern, variable )
print matches
add a comment |
import re
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
pattern = re.compile (";(.+?)~")
matches = re.findall ( pattern, variable )
print matches
add a comment |
import re
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
pattern = re.compile (";(.+?)~")
matches = re.findall ( pattern, variable )
print matches
import re
variable = ";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
pattern = re.compile (";(.+?)~")
matches = re.findall ( pattern, variable )
print matches
edited Dec 18 '18 at 19:04
answered Dec 18 '18 at 8:42
Raffi
1,04921120
1,04921120
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53829045%2fhow-to-split-each-individual-value-between-two-string-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
use split function . mystring.split(';') then mystirng.split('~')
– Hassan ALi
Dec 18 '18 at 8:37
3
Possible duplicate of How to split a string into a list? Note that the second answer indicates how to specify the delimiter.
– jpmc26
Dec 18 '18 at 18:14