Splitting string on new line after third float - Python
I'm trying to get a single string split into different lines.
I have the following single line string:
h =
John_______________7.3 7.9 9.7 Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5 Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
And i'm trying to get this output:
h =
John_______________7.3 7.9 9.7
Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5
Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
Where when i select h[0] the output should be:
John
Peter-Pan
Steve Stevenson
Johnny Palmer
Randy
I have already written code to work with the info if it's in the above format
I've tried for hours many different things but couldn't succeed. This let me to try to write new code to work with the info as it is right now instead.
I show my current code with that intention but it might not be relevant if the desired output is possible.
In the code I am trying to split a string on every name and three floats in a row. My current code now, though different from many other tries, isolates every three grades in a single item in a list and isolates the names as single items, but splits the names where they should not. I can't use iter, map and zip again since it's different with every name:
replacechar = h.replace(' ', '_')
student_list = replacechar.split('_')
isolated_grades = [item for item in student_list if
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names = [item for item in student_list if not
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names2 = ' '.join(isolated_names)
isolated_names3 = isolated_names2.split()
i = iter(isolated_grades)
f = map(" ".join, zip(i, i, i))
I tried splitting on delimeters like spaces or '_'. I tried replacing characters in order to split and tried defining every item based on .isalpha or .isdigit. However i cannot succeed since every name is of different length of words and might or might not contain spaces or dashes. Also the first float is connected with the name trough underscores. My brain is overloaded
Right now my goal is getting every person with grades on a new line which would allow me to select the names and grades:
python string list split
add a comment |
I'm trying to get a single string split into different lines.
I have the following single line string:
h =
John_______________7.3 7.9 9.7 Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5 Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
And i'm trying to get this output:
h =
John_______________7.3 7.9 9.7
Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5
Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
Where when i select h[0] the output should be:
John
Peter-Pan
Steve Stevenson
Johnny Palmer
Randy
I have already written code to work with the info if it's in the above format
I've tried for hours many different things but couldn't succeed. This let me to try to write new code to work with the info as it is right now instead.
I show my current code with that intention but it might not be relevant if the desired output is possible.
In the code I am trying to split a string on every name and three floats in a row. My current code now, though different from many other tries, isolates every three grades in a single item in a list and isolates the names as single items, but splits the names where they should not. I can't use iter, map and zip again since it's different with every name:
replacechar = h.replace(' ', '_')
student_list = replacechar.split('_')
isolated_grades = [item for item in student_list if
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names = [item for item in student_list if not
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names2 = ' '.join(isolated_names)
isolated_names3 = isolated_names2.split()
i = iter(isolated_grades)
f = map(" ".join, zip(i, i, i))
I tried splitting on delimeters like spaces or '_'. I tried replacing characters in order to split and tried defining every item based on .isalpha or .isdigit. However i cannot succeed since every name is of different length of words and might or might not contain spaces or dashes. Also the first float is connected with the name trough underscores. My brain is overloaded
Right now my goal is getting every person with grades on a new line which would allow me to select the names and grades:
python string list split
can you guarantee that no name includes a '_'
– Tony Suffolk 66
Nov 20 '18 at 22:46
yes (achieving minimal character limit)
– Sjoerd1234
Nov 20 '18 at 22:54
add a comment |
I'm trying to get a single string split into different lines.
I have the following single line string:
h =
John_______________7.3 7.9 9.7 Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5 Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
And i'm trying to get this output:
h =
John_______________7.3 7.9 9.7
Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5
Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
Where when i select h[0] the output should be:
John
Peter-Pan
Steve Stevenson
Johnny Palmer
Randy
I have already written code to work with the info if it's in the above format
I've tried for hours many different things but couldn't succeed. This let me to try to write new code to work with the info as it is right now instead.
I show my current code with that intention but it might not be relevant if the desired output is possible.
In the code I am trying to split a string on every name and three floats in a row. My current code now, though different from many other tries, isolates every three grades in a single item in a list and isolates the names as single items, but splits the names where they should not. I can't use iter, map and zip again since it's different with every name:
replacechar = h.replace(' ', '_')
student_list = replacechar.split('_')
isolated_grades = [item for item in student_list if
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names = [item for item in student_list if not
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names2 = ' '.join(isolated_names)
isolated_names3 = isolated_names2.split()
i = iter(isolated_grades)
f = map(" ".join, zip(i, i, i))
I tried splitting on delimeters like spaces or '_'. I tried replacing characters in order to split and tried defining every item based on .isalpha or .isdigit. However i cannot succeed since every name is of different length of words and might or might not contain spaces or dashes. Also the first float is connected with the name trough underscores. My brain is overloaded
Right now my goal is getting every person with grades on a new line which would allow me to select the names and grades:
python string list split
I'm trying to get a single string split into different lines.
I have the following single line string:
h =
John_______________7.3 7.9 9.7 Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5 Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
And i'm trying to get this output:
h =
John_______________7.3 7.9 9.7
Peter-Pan__________5.1 6.3 6.6
Steve Stevenson____5.1 5.3 5.5
Johnny Palmer______8.3 8.8 9.2
Randy______________8.0 8.0 8.0
Where when i select h[0] the output should be:
John
Peter-Pan
Steve Stevenson
Johnny Palmer
Randy
I have already written code to work with the info if it's in the above format
I've tried for hours many different things but couldn't succeed. This let me to try to write new code to work with the info as it is right now instead.
I show my current code with that intention but it might not be relevant if the desired output is possible.
In the code I am trying to split a string on every name and three floats in a row. My current code now, though different from many other tries, isolates every three grades in a single item in a list and isolates the names as single items, but splits the names where they should not. I can't use iter, map and zip again since it's different with every name:
replacechar = h.replace(' ', '_')
student_list = replacechar.split('_')
isolated_grades = [item for item in student_list if
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names = [item for item in student_list if not
item.strip('abcdefghijklmnopqrstuvwxyz_-0123456789') == '.']
isolated_names2 = ' '.join(isolated_names)
isolated_names3 = isolated_names2.split()
i = iter(isolated_grades)
f = map(" ".join, zip(i, i, i))
I tried splitting on delimeters like spaces or '_'. I tried replacing characters in order to split and tried defining every item based on .isalpha or .isdigit. However i cannot succeed since every name is of different length of words and might or might not contain spaces or dashes. Also the first float is connected with the name trough underscores. My brain is overloaded
Right now my goal is getting every person with grades on a new line which would allow me to select the names and grades:
python string list split
python string list split
edited Nov 20 '18 at 22:53
Sjoerd1234
asked Nov 20 '18 at 22:32
Sjoerd1234Sjoerd1234
6628
6628
can you guarantee that no name includes a '_'
– Tony Suffolk 66
Nov 20 '18 at 22:46
yes (achieving minimal character limit)
– Sjoerd1234
Nov 20 '18 at 22:54
add a comment |
can you guarantee that no name includes a '_'
– Tony Suffolk 66
Nov 20 '18 at 22:46
yes (achieving minimal character limit)
– Sjoerd1234
Nov 20 '18 at 22:54
can you guarantee that no name includes a '_'
– Tony Suffolk 66
Nov 20 '18 at 22:46
can you guarantee that no name includes a '_'
– Tony Suffolk 66
Nov 20 '18 at 22:46
yes (achieving minimal character limit)
– Sjoerd1234
Nov 20 '18 at 22:54
yes (achieving minimal character limit)
– Sjoerd1234
Nov 20 '18 at 22:54
add a comment |
1 Answer
1
active
oldest
votes
You could use regular expressions, which provide pattern matching. An regular expression of '[A-Za-z -]+_+[0-9. ]+'
should match the name, underscore, scores pattern. Then, re.findall('[A-Za-z -]+_+[0-9. ]+', string)
will return the list of strings. You can combine this back into a newline seperated string with 'n'.join(list_of_results)
.
Python regular expression documentation: https://docs.python.org/3/library/re.html
exactly what I would have done - but i wouldn't have been able to write the regex that quickly.
– Tony Suffolk 66
Nov 20 '18 at 22:47
1
You don't need to escape the dash in your character classes if it's the last character listed:[A-Za-z -]
– Jonah Bishop
Nov 20 '18 at 22:47
Sorry I'm revising my question due to a realization. Question remains somewhat the same though problem can be solved if the desired output can be achieved
– Sjoerd1234
Nov 20 '18 at 22:51
That actually simplifies things a little bit. I updated my answer for the revision.
– Neil Lindquist
Nov 20 '18 at 23:01
@JonahBishop Thanks, I'm never sure on the exact cases where the backslash can be skipped.
– Neil Lindquist
Nov 20 '18 at 23:03
|
show 1 more 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%2f53402581%2fsplitting-string-on-new-line-after-third-float-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use regular expressions, which provide pattern matching. An regular expression of '[A-Za-z -]+_+[0-9. ]+'
should match the name, underscore, scores pattern. Then, re.findall('[A-Za-z -]+_+[0-9. ]+', string)
will return the list of strings. You can combine this back into a newline seperated string with 'n'.join(list_of_results)
.
Python regular expression documentation: https://docs.python.org/3/library/re.html
exactly what I would have done - but i wouldn't have been able to write the regex that quickly.
– Tony Suffolk 66
Nov 20 '18 at 22:47
1
You don't need to escape the dash in your character classes if it's the last character listed:[A-Za-z -]
– Jonah Bishop
Nov 20 '18 at 22:47
Sorry I'm revising my question due to a realization. Question remains somewhat the same though problem can be solved if the desired output can be achieved
– Sjoerd1234
Nov 20 '18 at 22:51
That actually simplifies things a little bit. I updated my answer for the revision.
– Neil Lindquist
Nov 20 '18 at 23:01
@JonahBishop Thanks, I'm never sure on the exact cases where the backslash can be skipped.
– Neil Lindquist
Nov 20 '18 at 23:03
|
show 1 more comment
You could use regular expressions, which provide pattern matching. An regular expression of '[A-Za-z -]+_+[0-9. ]+'
should match the name, underscore, scores pattern. Then, re.findall('[A-Za-z -]+_+[0-9. ]+', string)
will return the list of strings. You can combine this back into a newline seperated string with 'n'.join(list_of_results)
.
Python regular expression documentation: https://docs.python.org/3/library/re.html
exactly what I would have done - but i wouldn't have been able to write the regex that quickly.
– Tony Suffolk 66
Nov 20 '18 at 22:47
1
You don't need to escape the dash in your character classes if it's the last character listed:[A-Za-z -]
– Jonah Bishop
Nov 20 '18 at 22:47
Sorry I'm revising my question due to a realization. Question remains somewhat the same though problem can be solved if the desired output can be achieved
– Sjoerd1234
Nov 20 '18 at 22:51
That actually simplifies things a little bit. I updated my answer for the revision.
– Neil Lindquist
Nov 20 '18 at 23:01
@JonahBishop Thanks, I'm never sure on the exact cases where the backslash can be skipped.
– Neil Lindquist
Nov 20 '18 at 23:03
|
show 1 more comment
You could use regular expressions, which provide pattern matching. An regular expression of '[A-Za-z -]+_+[0-9. ]+'
should match the name, underscore, scores pattern. Then, re.findall('[A-Za-z -]+_+[0-9. ]+', string)
will return the list of strings. You can combine this back into a newline seperated string with 'n'.join(list_of_results)
.
Python regular expression documentation: https://docs.python.org/3/library/re.html
You could use regular expressions, which provide pattern matching. An regular expression of '[A-Za-z -]+_+[0-9. ]+'
should match the name, underscore, scores pattern. Then, re.findall('[A-Za-z -]+_+[0-9. ]+', string)
will return the list of strings. You can combine this back into a newline seperated string with 'n'.join(list_of_results)
.
Python regular expression documentation: https://docs.python.org/3/library/re.html
edited Nov 20 '18 at 22:56
answered Nov 20 '18 at 22:46
Neil LindquistNeil Lindquist
18614
18614
exactly what I would have done - but i wouldn't have been able to write the regex that quickly.
– Tony Suffolk 66
Nov 20 '18 at 22:47
1
You don't need to escape the dash in your character classes if it's the last character listed:[A-Za-z -]
– Jonah Bishop
Nov 20 '18 at 22:47
Sorry I'm revising my question due to a realization. Question remains somewhat the same though problem can be solved if the desired output can be achieved
– Sjoerd1234
Nov 20 '18 at 22:51
That actually simplifies things a little bit. I updated my answer for the revision.
– Neil Lindquist
Nov 20 '18 at 23:01
@JonahBishop Thanks, I'm never sure on the exact cases where the backslash can be skipped.
– Neil Lindquist
Nov 20 '18 at 23:03
|
show 1 more comment
exactly what I would have done - but i wouldn't have been able to write the regex that quickly.
– Tony Suffolk 66
Nov 20 '18 at 22:47
1
You don't need to escape the dash in your character classes if it's the last character listed:[A-Za-z -]
– Jonah Bishop
Nov 20 '18 at 22:47
Sorry I'm revising my question due to a realization. Question remains somewhat the same though problem can be solved if the desired output can be achieved
– Sjoerd1234
Nov 20 '18 at 22:51
That actually simplifies things a little bit. I updated my answer for the revision.
– Neil Lindquist
Nov 20 '18 at 23:01
@JonahBishop Thanks, I'm never sure on the exact cases where the backslash can be skipped.
– Neil Lindquist
Nov 20 '18 at 23:03
exactly what I would have done - but i wouldn't have been able to write the regex that quickly.
– Tony Suffolk 66
Nov 20 '18 at 22:47
exactly what I would have done - but i wouldn't have been able to write the regex that quickly.
– Tony Suffolk 66
Nov 20 '18 at 22:47
1
1
You don't need to escape the dash in your character classes if it's the last character listed:
[A-Za-z -]
– Jonah Bishop
Nov 20 '18 at 22:47
You don't need to escape the dash in your character classes if it's the last character listed:
[A-Za-z -]
– Jonah Bishop
Nov 20 '18 at 22:47
Sorry I'm revising my question due to a realization. Question remains somewhat the same though problem can be solved if the desired output can be achieved
– Sjoerd1234
Nov 20 '18 at 22:51
Sorry I'm revising my question due to a realization. Question remains somewhat the same though problem can be solved if the desired output can be achieved
– Sjoerd1234
Nov 20 '18 at 22:51
That actually simplifies things a little bit. I updated my answer for the revision.
– Neil Lindquist
Nov 20 '18 at 23:01
That actually simplifies things a little bit. I updated my answer for the revision.
– Neil Lindquist
Nov 20 '18 at 23:01
@JonahBishop Thanks, I'm never sure on the exact cases where the backslash can be skipped.
– Neil Lindquist
Nov 20 '18 at 23:03
@JonahBishop Thanks, I'm never sure on the exact cases where the backslash can be skipped.
– Neil Lindquist
Nov 20 '18 at 23:03
|
show 1 more 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.
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%2f53402581%2fsplitting-string-on-new-line-after-third-float-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
can you guarantee that no name includes a '_'
– Tony Suffolk 66
Nov 20 '18 at 22:46
yes (achieving minimal character limit)
– Sjoerd1234
Nov 20 '18 at 22:54