Python Forum
Grabbing a Subset of a String
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grabbing a Subset of a String
#1
I need to create a new field we can call Date_Time_txt, which is based off of a long string (!FolderPath!). Example of the full string: "Call Loc/Folder/(555) 555-5555 - 2/27/2019 8:52:34 AM/RoomB". I initially started the code as Date_Time_txt= !FolderPath![42:64], which would start at the date and grab the time, but because of varying time and date characters, I also grab "/R" on some sets. How can I run the function to just grab the date and time?
Reply
#2
I would go with a regular expression (the re module). Regular expressions are used in tons of languages, so if you did a web search for "date regular expression" I'm sure you would find one fitting your requirements.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jun-18-2019, 02:26 PM)ichabod801 Wrote: I would go with a regular expression (the re module). Regular expressions are used in tons of languages, so if you did a web search for "date regular expression" I'm sure you would find one fitting your requirements.
Thanks!
Reply
#4
If structure of strings is similar then one can achieve desired result also without re

>>> s = "Call Loc/Folder/(555) 555-5555 - 2/27/2019 8:52:34 AM/RoomB"
>>> " ".join(s.rsplit("/", maxsplit=1)[0].rsplit(maxsplit=3)[-3:])
'2/27/2019 8:52:34 AM'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  bug in subset sum algorithm usercat123 3 1,337 Feb-07-2022, 05:41 AM
Last Post: deanhystad
  how to refer to a subset of a list Skaperen 4 1,581 Jan-16-2022, 02:16 AM
Last Post: Skaperen
  How to create subset in python? Bhavika 5 1,853 Nov-27-2021, 07:16 PM
Last Post: Gribouillis
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 3,931 Apr-10-2020, 02:39 PM
Last Post: buran
  Grabbing a value from one python script into another nickayres 3 3,233 May-24-2019, 08:21 PM
Last Post: snippsat
  Grabbing questions from a quiz app CodingUnicorn 0 1,929 Mar-05-2019, 11:26 PM
Last Post: CodingUnicorn

Forum Jump:

User Panel Messages

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