changeset 68:f02036f75ecf draft

Implement demonstration CSV parser
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 25 May 2019 14:50:05 +0200
parents 2253177294ab
children f6c53dea71f2
files pcombinators/csv_test.py test.csv
diffstat 2 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcombinators/csv_test.py	Sat May 25 14:50:05 2019 +0200
@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Created on Fri May 24 23:46:48 2019
+
+@author: lbo
+"""
+
+from pcombinators import *
+
+separator = Whitespace() + OneOf(",") + Whitespace()
+string = Skip(String('"')) + NoneInSet('"') + Skip(String('"'))
+integer = Last(Integer() + Skip((Peek(NoneInSet('.')) | EndOfInput())))
+value = integer | Float() | Last(string)
+line = Flatten(Repeat(OptimisticSequence(value, Skip(separator)), -1)).then_skip((String('\n') | EndOfInput()))
+
+file = Repeat(line, -1)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test.csv	Sat May 25 14:50:05 2019 +0200
@@ -0,0 +1,4 @@
+1, 2, 3
+"hello", "world"
+"hello", "world"
+"hello", 1, 1.44