Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create list from String
#1
Hello All,

I am new to Python. I have installed python recently few weeks back and started exploring it . I need one suggestion for from which book I should study . Considering I am database person no industry experience in Java and all.

Ok So my main question is I have string like this :

b'{"rawInput":"","cc":1,"nationalNumber":11111,"cc_source":"FROM_NUMBER_WITH_PLUS_SIGN","extension":"","italianLeadingZero":false,"region":null,"numberType":"UNKNOWN","e164Format":"+111111","rfc3966Format":"tel:+1-11111","originalFormat":"11111","nationalFormat":"+1 11111","internationalFormat":null,"outOfCountryFormatFromUS":"1 11111","outOfCountryFormatFromCH":"00 1 11111","location":"","possibleNumber":false,"validNumber":false,"validNumberForRegion":false}'

and I want to create list from it like this

[rawInput,null,cc,1,nationalNumber,11111,cc_source,FROM_NUMBER_WITH_PLUS_SIGN......,
validNumberForRegion,false]

Can u please help me how Can I do this .
Reply
#2
Thread moved to general coding help.

What you are looking for is the split method of strings:

>>> 'a:b:c'.split(':')
['a', 'b', 'c']
However, note that your string has a b before it. That means it is a binary string. So you will need to pass a binary string (like b':') to the split method.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
This is in json format, so you need to:
  • assign your binary data to a variable
  • convert to text - use variablename.decode("utf-8")
  • load result into a dictionary using json.loads
  • create empty list with []
  • iterate through dictionary, extracting key, value pairs
  • append value to empty list created above
Reply
#4
Thanks Everyone for Help.
larz60+ your step by step explanation helped me in solving my problem
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,430 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  how to easily create a list of already existing item CompleteNewb 15 3,386 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Create SQLite columns from a list or tuple? snakes 6 8,522 May-04-2021, 12:06 PM
Last Post: snakes
  Create variable and list dynamically quest_ 12 4,289 Jan-26-2021, 07:14 PM
Last Post: quest_
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,773 Dec-15-2020, 05:21 PM
Last Post: snippsat
  How to create a linked list and call it? loves 12 4,369 Nov-22-2020, 03:50 PM
Last Post: loves
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 1,989 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  How to define a function to create a resorted list? sparkt 6 2,741 Aug-08-2020, 04:10 PM
Last Post: sparkt
  Create array from string steve87bg 4 3,115 Jul-13-2020, 07:55 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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