Skip to content

Commit c8a000f

Browse files
committed
fix: fix export function
1 parent 4f1ed93 commit c8a000f

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

scrapegraphai/utils/data_export.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import json
2+
import csv
3+
import xml.etree.ElementTree as ET
4+
from typing import List, Dict, Any
5+
6+
def export_to_json(data: List[Dict[str, Any]], filename: str) -> None:
7+
"""
8+
Export data to a JSON file.
9+
10+
:param data: List of dictionaries containing the data to export
11+
:param filename: Name of the file to save the JSON data
12+
"""
13+
with open(filename, 'w', encoding='utf-8') as f:
14+
json.dump(data, f, ensure_ascii=False, indent=4)
15+
print(f"Data exported to {filename}")
16+
17+
def export_to_csv(data: List[Dict[str, Any]], filename: str) -> None:
18+
"""
19+
Export data to a CSV file.
20+
21+
:param data: List of dictionaries containing the data to export
22+
:param filename: Name of the file to save the CSV data
23+
"""
24+
if not data:
25+
print("No data to export")
26+
return
27+
28+
keys = data[0].keys()
29+
with open(filename, 'w', newline='', encoding='utf-8') as f:
30+
writer = csv.DictWriter(f, fieldnames=keys)
31+
writer.writeheader()
32+
writer.writerows(data)
33+
print(f"Data exported to {filename}")
34+
35+
def export_to_xml(data: List[Dict[str, Any]], filename: str, root_element: str = "data") -> None:
36+
"""
37+
Export data to an XML file.
38+
39+
:param data: List of dictionaries containing the data to export
40+
:param filename: Name of the file to save the XML data
41+
:param root_element: Name of the root element in the XML structure
42+
"""
43+
root = ET.Element(root_element)
44+
for item in data:
45+
element = ET.SubElement(root, "item")
46+
for key, value in item.items():
47+
sub_element = ET.SubElement(element, key)
48+
sub_element.text = str(value)
49+
50+
tree = ET.ElementTree(root)
51+
tree.write(filename, encoding='utf-8', xml_declaration=True)
52+
print(f"Data exported to {filename}")
53+

0 commit comments

Comments
 (0)