Re: Why Strict YAML Refuses To Do Implicit Typing

Liste des GroupesRevenir à c misc 
Sujet : Re: Why Strict YAML Refuses To Do Implicit Typing
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.misc
Date : 24. Apr 2024, 13:14:22
Autres entêtes
Organisation : Stefan Ram
Message-ID : <notation-20240424131100@ram.dialup.fu-berlin.de>
References : 1
Ben Collver <bencollver@tilde.pink> wrote or quoted:
- GB
- IE
- FR
- DE

  These days JSON is ubiquitous on the web. In the programming world,
  it's Python that rules. Check this out - the following notation is
  valid JSON as well as valid Python, and it's so straightforward that
  most people could understand it after a bit of training.

{ "countries":
  [ "GB",
    "IE",
    "FR",
    "DE" ]}

  Here's a script that parses the same string (like it could
  have been read from a config file) once as JSON and once
  as Python - the result is the same.

  Main.py

# This is a script for Python 3.9.

# Prepare that input like it's a string that got read from a file.
input = '''
{ "countries":
  [ "GB",
    "IE",
    "FR",
    "DE" ]}
'''[ 1: -1 ]
print( f"input is (without the outer apostrophes):\n'{input}'" )

print( "\nReading input as json:" )
import json
print( json.loads( input ))

print( "\nReading input as Python:" )
import ast
print( ast.literal_eval( input ))

  Output

input is (without the outer apostrophes):
'{ "countries":
  [ "GB",
    "IE",
    "FR",
    "DE" ]}'

Reading input as json:
{'countries': ['GB', 'IE', 'FR', 'DE']}

Reading input as Python:
{'countries': ['GB', 'IE', 'FR', 'DE']}

  So the input got converted into a Python dict, which contains
  a Python list, which in turn holds Python strs (strings).

Date Sujet#  Auteur
24 Apr 24 * Why Strict YAML Refuses To Do Implicit Typing6Ben Collver
24 Apr 24 +* Re: Why Strict YAML Refuses To Do Implicit Typing2Stefan Ram
25 Apr 24 i`- Re: Why Strict YAML Refuses To Do Implicit Typing1Lawrence D'Oliveiro
24 Apr 24 +- Re: Why Strict YAML Refuses To Do Implicit Typing1Stefan Ram
25 Apr 24 `* Re: Why Strict YAML Refuses To Do Implicit Typing2Blue-Maned_Hawk
25 Apr 24  `- Re: Why Strict YAML Refuses To Do Implicit Typing1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal