Package: csvmeta
CSVMetaCSVMetais an extremely lightweight Python package designed to work with csv files and attached metadata. It writes data to an arbitrary folder such asmydata.csv/and creates two internal files:mydata.csv/data.csv: the usual csv file.mydata.csv/metadata.json: metadata about the csv file, such as thecsv dialect.When reading data frommydata.csv/, it uses dialect information from the metadata file to read the csv data correctly. The metadata file can also be used to store additional information about the data, such as a data schema and a header indicator.The package has no external dependencies beyond Python's standard library and is tested with Python 3.7+ on Linux, Windows, and macOS.InstallationpipinstallcsvmetaUsageReading and Writing DataInput and ouput data formats for thereadandwritefunctions are modelled on Python's csv module: data to write should be an iterable of rows, and data read will be an iterable of rows with string data types. The data header is always returned as the first row.importcsvmetaascsvmdata=[['name','age','state'],['Nicole',43,'CA'],['John',28,'DC']]# Write data to a csv file foldercsvm.write('mydata.csv',data)# Read data from a csv file folderdata=csvm.read('mydata.csv')## [##     ['name', 'age', 'state'],##     ['Nicole', '43', 'CA'],##     ['John', '28', 'DC']## ]##Reading and Writing MetadataMetadata is stored in a json file in the csv folder. The metadata file is created automatically when writing data, and only thedialectobject is used when reading data. Thedialectobject is a dictionary of csv dialect parameters, such asdelimiter,quotechar, andlineterminator. See thecsv module documentationfor more information.Arbitrary metadata can be added to the metadata file by passing keyword arguments to thewritefunction. We recommend setting theheaderkeyword argument toTrueif the first row of the data is a header row, and setting theschemakeyword argument to a list of column names and data types. Thefrictionless tabular data resource standardis a good reference for metadata schemas.Metadata can be read using themetadata()functionimportcsvmetaascsvmdata=[['name','age','state'],['Nicole',43,'CA'],['John',28,'DC']]# Write data and metadata to a csv file foldercsvm.write('mydata.csv',data,header=True,schema=['name','age','state'],dialect={'delimiter':',','quotechar':'"','lineterminator':'\n'},description='This is an example dataset.')# Read metadata from a csv file foldercsvm.metadata('mydata.csv')## {##     "name": "mydata.csv",##     "path": "data.csv",##     "mediatype": "text/csv",##     "dialect": {##         "delimiter": ",",##         "quotechar": "\"",##         "lineterminator": "\n"##     },##     "header": true,##     "schema": [##         "name",##         "age",##         "state"##     ],##     "description": "This is an example dataset."## }Reading to Pandas DataFrameimportcsvmetaascsvmdata=[['name','age','state'],['Nicole',43,'CA'],['John',28,'DC']]# Write data and metadata to a csv file foldercsvm.write('mydata.csv',data,header=True)data=csvm.read('mydata.csv')metadata=csvm.metadata('mydata.csv')ifmetadata.get("header",False):df=pd.DataFrame(data[1:],columns=data[0])else:df=pd.DataFrame(data)df##      name age state## 0  Nicole  43    CA## 1    John  28    DCLinks and ReferencesCSV Module DocumentationFrictionless Tabular Data Resource StandardCommon Format and MIME Type for Comma-Separated Values (CSV) FilesCSV on the WebChangelog1.1.2 (2023-11-25)MakeDEFAULT_DIALECTan explicit dictionary specification rather than "unix".AddDEFAULT_DIALECTto tests.1.1.1 (2023-11-25)ChangeIterabletyping toSequenceto account for order and allow multiple passes over data.Improve tests.1.1.0 (2023-11-25)Fix read function return type: now return list of lists instead of generator.1.0.0 (2023-11-25)Initial release.