Python Forum
Split string using variable found in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split string using variable found in a list
#1
Hello,
I have to split a string using a variable found in a list, but it doesn't work. This is my code:
causali = ['IP', 'TP', 'PR1', 'PR2', 'PR3', 'TP', 'BC', 'BF', 'VP', 'RP']
file = ('12011191-92-95-202-203_220701_PR1 - Copia')
print (file)

causale=str([ele for ele in causali if ele in file])
print (causale)

ls=file.split(causale)
print (ls)

and this is my output:

12011191-92-95-202-203_220701_PR1 - Copia
['PR1']
['12011191-92-95-202-203_220701_PR1 - Copia']

What's wrong?
Reply
#2
causali = ['IP', 'TP', 'PR1', 'PR2', 'PR3', 'TP', 'BC', 'BF', 'VP', 'RP']
file = ('12011191-92-95-202-203_220701_PR1 - Copia')
print (file)

causale=[ele for ele in causali if ele in file]
print (causale)

ls=file.split(causale[0])
print (ls)
Output:
12011191-92-95-202-203_220701_PR1 - Copia ['PR1'] ['12011191-92-95-202-203_220701_', ' - Copia']
Reply
#3
Thank you very much, perfect!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,522 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,135 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Replacing String Variable with a new String Name kevv11 2 787 Jul-29-2023, 12:03 PM
Last Post: snippsat
  [split] Parse Nested JSON String in Python mmm07 4 1,531 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Need help on how to include single quotes on data of variable string hani_hms 5 2,033 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,825 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,521 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  USE string data as a variable NAME rokorps 1 967 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,294 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,784 Jul-27-2022, 02:36 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