Python Forum
Splitting strings in list of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting strings in list of strings
#1
Hi, trying to figure this one out which I expect should be easy. If I find my own solution before anyone else I'll post it here. I'm looking. Thanks

I have successfully split my line for values that were in quotes:

"val_1: val_2" "val_3: val_4" "val_5: val_6"

Now I have a list that contains: (which I think is a list of strings)

[ 'val_1: val_2' , 'val_3: val_4' , 'val_5: val_6']
How can I print the 2nd value in the pair separated by the colon :

To produce:

val_2 , val_4, val_6
Reply
#2
Got it.

result = [ 'val_1: val_2' , 'val_3: val_4' , 'val_5: val_6']
for i in result:
    sword = i.split(':')
    print (sword[1])   
Result:

val_2
val_4
val_6
Yes?
Yes!
Reply
#3
Please show your code (enough for us to run, please)

What you show in the first list appears to be a dictionary represented as a list of strings.

If so the method of access of second item value is simple using original dictionary.
Need to see original form of data, prior to your conversion in order to determine if this is possible.
Reply
#4
I think a regex could help.

I used this regex: r"\s+?(\w+)\s+?:\s+?(\w+)"
On this line of text: " val_1 : val_2 val_3 : val_4 val_5 : val_6 "
I put the white spaces intentionally in this long string to make it harder.


You can the regex on https://regex101.com/
Don't forget to check Python as language.
The regex-string is on the top, and you must enter it without the leading r and the quotes.

The function re.findall will return all matching groups.
A dict can take this output.

Doing the same with str.split, str.strip etc. is a bit harder.
For myself, I didn't find a good solution without regex for this problem.

Often, the use of a regex can lead into the wrong direction.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
  Bug: Two Python unistall strings available on Windows 10 pstein 2 668 Oct-26-2025, 11:24 AM
Last Post: noisefloor
  concatenating strings with + operator CarlaRogersWI 6 1,565 Aug-10-2025, 12:23 PM
Last Post: noisefloor
  sending strings to arduino over serial/accessing DLLs HeWhoHas 0 1,416 Nov-09-2024, 06:01 PM
Last Post: HeWhoHas
  Can you explain the strings in Python ebn852_pan 3 1,761 May-19-2024, 08:36 AM
Last Post: Pedroski55
Information Do regular expressions still need raw strings? bobmon 3 5,216 May-03-2024, 09:05 AM
Last Post: rishika24
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 1,947 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Tab Delimited Strings? johnywhy 7 3,275 Jan-13-2024, 10:34 PM
Last Post: sgrey
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 2,203 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  How to read module/class from list of strings? popular_dog 1 1,902 Oct-04-2023, 03:08 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020