Python Forum
Using tuples or Re to replace 5 strings with one
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using tuples or Re to replace 5 strings with one
#1
Hi, I am trying to simplify a list of fruits in one file, by generalizing them to "fruit". However, I can only using regular tuples and re, replace one by one. Here I would need to replace 4 different words with one, as the example shows below. But it doesn't work. Any ideas?

Thanks

fruit = ("Apple", "Orange", "Banana", "Mandarin")
fruit_trans = {}
fruit_trans = [("fruit")]
trans = string.maketrans("Apple", "Orange", "Banana", "Mandarin")
my_string = my_string.translate(trans)
Reply


Messages In This Thread
Using tuples or Re to replace 5 strings with one - by sem - Aug-23-2017, 11:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 790 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,802 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,534 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Search & Replace - Newlines Added After Replace dj99 3 3,426 Jul-22-2018, 01:42 PM
Last Post: buran
  lists, strings, and byte strings Skaperen 2 4,257 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  search and replace first amount of strings instances with one thing and a second amou chickflick91 7 5,600 Sep-26-2017, 05:13 PM
Last Post: chickflick91
  replace specific words in strings Sama 3 3,681 Jul-23-2017, 01:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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