Python Forum
Help with converting strings to arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with converting strings to arrays
#2
Let us assume you can not be sure that each item is an integer and all are seperate through a comma.
The simplest solution assuming that each is an integer is:
row_arr = row[0].split(',')
row_arr = [int(value) for value in row_arr]
If you can not be sure that each is a string, you should consider regex:
import re
your_regex = re.compile('([0-9]+)')
row_array = row_array[0].split(',')
row_array = [int(your_regex.match(value).group(1)) for value in row_array if your_regex.match(value) is not None]
what this does is to check if you string is matching this pattern, which defines each number greater than 0. If you find a string like "a" your regex will return None and you may skip it :)
Reply


Messages In This Thread
RE: Help with converting strings to arrays - by ThiefOfTime - May-03-2018, 06:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 899 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,891 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,601 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,321 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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