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
#2
The best way to save lists and/or dictionaries is as a json file.
in your code, this would be done as follows:

import json


fruit = ["Apple", "Orange", "Banana", "Mandarin"]
# this can be accessed by index:
print('fruit[0]: {}, fruit[2]: {}'.format(fruit[0], fruit[2]))
print('fruit: {}'.format(fruit))

# To save to file named fruit.dat:
with open('fruit.dat', 'w') as fout:
    json.dump(fruit, fout)

# To load back in:
with open('fruit.dat') as fin:
    newfruit = json.load(fin)
print('newfruit: {}'.format(newfruit))
output:
Output:
fruit[0]: Apple, fruit[2]: Banana fruit: ['Apple', 'Orange', 'Banana', 'Mandarin'] newfruit: ['Apple', 'Orange', 'Banana', 'Mandarin']
Reply


Messages In This Thread
RE: Using tuples or Re to replace 5 strings with one - by Larz60+ - Aug-23-2017, 11:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 807 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,814 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,543 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Search & Replace - Newlines Added After Replace dj99 3 3,430 Jul-22-2018, 01:42 PM
Last Post: buran
  lists, strings, and byte strings Skaperen 2 4,259 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,614 Sep-26-2017, 05:13 PM
Last Post: chickflick91
  replace specific words in strings Sama 3 3,687 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