From 10ed5d808f1be9c1b467af216c1a910c0d891700 Mon Sep 17 00:00:00 2001 From: richiverse Date: Mon, 6 May 2019 13:38:14 -0400 Subject: [PATCH] update csv import --- pandas_ext/__init__.py | 6 ++++-- pandas_ext/_xml.py | 32 +++++++++++++++++++++++++++++++ pandas_ext/amazon_spectrum.py | 1 - pandas_ext/{_csv.py => px_csv.py} | 0 setup.py | 1 + 5 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 pandas_ext/_xml.py rename pandas_ext/{_csv.py => px_csv.py} (100%) diff --git a/pandas_ext/__init__.py b/pandas_ext/__init__.py index fc0e05b..4829a36 100644 --- a/pandas_ext/__init__.py +++ b/pandas_ext/__init__.py @@ -1,5 +1,5 @@ """Versioning kept here.""" -__version__ = '0.4.13' +__version__ = '0.5.01' __license__ = "MIT" __title__ = "pandas_ext" @@ -15,7 +15,7 @@ del _pd from .amazon_spectrum import to_spectrum -from ._csv import to_csv +from .px_csv import to_csv from .excel import to_excel from .gdrive import read_gdrive, to_gdrive from .parquet import read_parquet, to_parquet @@ -23,6 +23,7 @@ read_sfdc, sfdc_metadata, patch_sfdc, async_patch_sfdc, ) from .sql import read_sql, list_backends +from .snowflake import read_snowflake, to_snowflake @@ -35,3 +36,4 @@ del parquet del sfdc del sql +del snowflake diff --git a/pandas_ext/_xml.py b/pandas_ext/_xml.py new file mode 100644 index 0000000..82fd493 --- /dev/null +++ b/pandas_ext/_xml.py @@ -0,0 +1,32 @@ +"""Write a pandas dataframe to xml.""" +import pandas as pd +from xml.sax.saxutils import escape + + +def to_xml(df, filename=None, mode='w'): + """Write df to xml.""" + def row_to_xml(row): + """Row by row.""" + xml = [''] + for i, col_name in enumerate(row.index): + raw = row.iloc[i] + if not isinstance(raw, float) and '&' in raw: + print(raw, type(raw)) + value = escape(raw) if isinstance(raw, str) else raw + + xml.append(' {1}' + .format(col_name, value) + ) + xml.append('') + return '\n'.join(xml) + + res = '\n' + res += '\n'.join(df.apply(row_to_xml, axis=1)) + + if filename is None: + return res + with open(filename, mode) as f: + f.write(res) + + +pd.DataFrame.to_xml = to_xml diff --git a/pandas_ext/amazon_spectrum.py b/pandas_ext/amazon_spectrum.py index 72be2a0..2050ce8 100644 --- a/pandas_ext/amazon_spectrum.py +++ b/pandas_ext/amazon_spectrum.py @@ -1,7 +1,6 @@ from inspect import cleandoc import pandas as pd -print(dir(pd)) from pandas_ext.common.utils import today from pandas_ext.sqla_utils import schema_from_df diff --git a/pandas_ext/_csv.py b/pandas_ext/px_csv.py similarity index 100% rename from pandas_ext/_csv.py rename to pandas_ext/px_csv.py diff --git a/setup.py b/setup.py index 97bf40d..cdcdf5f 100644 --- a/setup.py +++ b/setup.py @@ -64,6 +64,7 @@ def find_meta(meta): xls=["xlwt"], xlsx=["openpyxl", "xlsxwriter"], snowflake=["snowflake-sqlalchemy"], + parquet=["pyarrow"], ), long_description=README, packages=PACKAGES,