Add a CSV to OPML example.

This commit is contained in:
Bauke 2022-09-26 10:34:34 +02:00
parent cc8bd20a54
commit a9f357cf0c
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 29 additions and 0 deletions

3
examples/csv-to-opml.csv Normal file
View File

@ -0,0 +1,3 @@
Text,Url
Example,https://example.org
Another Example,https://example.org/another
1 Text Url
2 Example https://example.org
3 Another Example https://example.org/another

26
examples/csv-to-opml.py Normal file
View File

@ -0,0 +1,26 @@
import csv
from opyml import OPML, Outline
def main() -> None:
document = OPML()
with open("examples/csv-to-opml.csv", "r") as example_csv:
csv_reader = csv.DictReader(example_csv)
for row in csv_reader:
document.body.outlines.append(
Outline(
# Convert to str because text is a required OPML attribute
# and row.get() can return None.
text=str(row.get("Text")),
xml_url=row.get("Url"),
)
)
print(document.to_xml())
if __name__ == "__main__":
main()