Questions tagged [multi-document]

Use this tag for questions that are related to multiple documents in one file. multi-document can be format specified (e.g. YAML), or by repeating a format that can be expressed in single line (JSON)

Multiple documents in one file are often used to keep together information that is related but doesn't necessarily be processed in the same way. It has the advantage over e.g. having multiple files with different suffices (.meta, .doc) that the documents cannot be separated on the filesystem level.

Some formats have multi-documents built in from their specification (e.g. YAML). But essentially any format that can be separated by a character sequence that cannot occur as part of the format itself, or for which an explicit or implicit "closing" can be detected (e.g JSON) can be combined into a multi document file.

One multi-document form for JSON is where each document is a single line, which can be separetely parsed.

A routine to deal with such combination multi-line documents can be written on top of a standard parser for such documents, and libraries that provide that functionality are e.g. available for Python (multiline, json-lines).

8 questions
21
votes
2 answers

How to parse a YAML file with multiple documents?

Here is my parsing code: import yaml def yaml_as_python(val): """Convert YAML to dict""" try: return yaml.load_all(val) except yaml.YAMLError as exc: return exc with open('circuits-small.yaml','r') as input_file: …
BigBoy1337
  • 4,735
  • 16
  • 70
  • 138
2
votes
1 answer

How do I read/write markdown yaml frontmatter with ruamel.yaml?

I want to use Python to read and write YAML frontmatter in markdown files. I have come across the ruamel.yaml package but am having trouble understanding how to use it for this purpose. If I have a markdown file: --- car: make: Toyota model:…
pruppert
  • 111
  • 1
  • 12
2
votes
1 answer

YAML 1.2 directive with multiple documents doesn't work in unsafe mode

I'm trying to load a multi-document YAML config file like the following: file: %YAML 1.2 --- num_epochs: 1 --- num_epochs: 1 and the python script is: from ruamel.yaml import YAML yaml = YAML(typ='unsafe') configs =…
episodeyang
  • 642
  • 8
  • 15
1
vote
1 answer

How can I maintain formatting while updating a yaml file using ruamel?

I have a multi-document YAML file. I am interested in modifying the third document only (this modification will be later made using other code and conditions). After some research, I selected ruamel since it was reported to preserve order and…
HOSSAM
  • 37
  • 7
0
votes
1 answer

WriteConflict error in MongoDB when running batches of insertMany operation with transaction from Quarkus

Below is a snippet of my transaction implementation. Currently MongoDB will throw WriteConflict error every time when it is processing the records from 800k onwards. ClientSession clientSession = mongoClient.startSession(); …
Zhen Wei
  • 339
  • 1
  • 3
  • 3
0
votes
2 answers

How to edit a file with multiple YAML documents in Python

I have the following YAML file: apiVersion: apps/v1 kind: Deployment metadata: name: nodejs namespace: test labels: app: hello-world spec: selector: matchLabels: app: hello-world replicas: 100 template: metadata: …
microset
  • 276
  • 1
  • 2
  • 12
0
votes
1 answer

Write multiple JSON lines to JSON file

I have a code that needs to read a JSON file with multiple lines, i.e: {"c1-line1": "value", "c2-line1": "value"} {"c1-line2": "value", "c2-line2": "value"}... and, after change the keys values (already working), I need to write a new json file…
IanPoli
  • 99
  • 8
0
votes
1 answer

Format JSON to one line per input

I'm trying to make my json compatible with Google ML, which after some searching means having one line per input instance (Input instances are not in JSON format.) How can I change my code to have only one line per instance? (img variable is the…
K41F4r
  • 1,443
  • 1
  • 16
  • 36