@@ -79,54 +79,33 @@ which are then stored in a ZIP or TAR archive.
7979import jsonl
8080
8181data = [
82+ # Create `file1.jsonl` withing the archive
8283 (" file1.jsonl" , [{" name" : " Alice" , " age" : 30 }, {" name" : " Bob" , " age" : 25 }]),
84+ # Create `file2.jsonl` within the archive
8385 (" path/to/file2.jsonl" , [{" name" : " Charlie" , " age" : 35 }, {" name" : " David" , " age" : 40 }]),
84- (" file1.jsonl" , [{" name" : " Eve" , " age" : 28 }]), # this will append to the file1.jsonl
86+ # Append to `file1.jsonl` within the archive
87+ (" file1.jsonl" , [{" name" : " Eve" , " age" : 28 }]),
8588]
8689jsonl.dump_archive(" my_archive.zip" , data)
8790```
8891
8992** Dumping data to Multiple JSON Lines Files**
9093
91- Use ` jsonl.dump_fork ` to incrementally write structured data to multiple ** .jsonl** files—one per key (in this case, player name).
92- This helps organize and efficiently store data for separate entities.
93- This example creates individual JSON Lines files for each player, storing their respective wins.
94+ Use ` jsonl.dump_fork ` to incrementally write structured data to multiple ** .jsonl** files,
95+ which can be useful when you want to separate data based on some criteria.
9496
9597``` python
9698import jsonl
9799
98-
99- def generate_win_data ():
100- """ Yield player wins data for multiple players."""
101-
102- data = (
103- {
104- " name" : " Gilbert" ,
105- " wins" : [
106- {" hand" : " straight" , " card" : " 7♣" },
107- {" hand" : " one pair" , " card" : " 10♥" },
108- ]
109- },
110- {
111- " name" : " May" ,
112- " wins" : [
113- {" hand" : " two pair" , " card" : " 9♠" },
114- ]
115- },
116- {
117- " name" : " Gilbert" ,
118- " wins" : [
119- {" hand" : " three of a kind" , " card" : " A♦" },
120- ]
121- }
122- )
123- for player in data:
124- name = player[" name" ]
125- yield (f " { name} .jsonl " , player[" wins" ])
126-
127-
128- # Write the generated data to files in JSON Lines format
129- jsonl.dump_fork(generate_win_data())
100+ data = [
101+ # Create `file1.jsonl` or overwrite it if it exists
102+ (" file1.jsonl" , [{" name" : " Alice" , " age" : 30 }, {" name" : " Bob" , " age" : 25 }]),
103+ # Create `file2.jsonl` or overwrite it if it exists
104+ (" file2.jsonl" , [{" name" : " Charlie" , " age" : 35 }, {" name" : " David" , " age" : 40 }]),
105+ # Append to `file1.jsonl`
106+ (" file1.jsonl" , [{" name" : " Eve" , " age" : 28 }]),
107+ ]
108+ jsonl.dump_fork(data)
130109```
131110
132111## Documentation
0 commit comments