Splitting string on new line after third float - Python












2















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:










share|improve this question

























  • 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
















2















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:










share|improve this question

























  • 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














2












2








2








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:










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















3














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






share|improve this answer


























  • 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











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


}
});














draft saved

draft discarded


















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









3














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






share|improve this answer


























  • 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
















3














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






share|improve this answer


























  • 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














3












3








3







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?