26

Is there a way to add a "tag" (add a unique metadata/identifier) to a CSV file without affecting the contents or ability to read/write the file?

I am using Python, but I don't think the language matters here.

mhy
  • 455
  • 2
  • 6
  • 12
  • 2
    You might want to look at this post:http://stackoverflow.com/questions/14158868/python-skip-comment-lines-marked-with-in-csv-dictreader – bitfish Aug 21 '13 at 16:29
  • 1
    I'd vote for the creation of a whole new file format that weaves both together. Not sure how that would hit a lot of OS and tool infrastructures in a breaking way but if it could be done in a non-breaking way I'd definitely be an adopter. I've had many woes to CSV files not having metadata outside of maybe tucking it into the file name. – jxramos Aug 06 '15 at 21:39

3 Answers3

21

This would be compliant with W3C embedded metadata format: http://www.w3.org/TR/tabular-data-model/#embedded-metadata

#publisher,W3C
#updated,2015-10-17T00:00:00Z
#name,sensor,temperature
#datatype,string,float
sensor,temperature
s-1,25.5
Sergei Rodionov
  • 4,079
  • 6
  • 27
  • 44
17

Just add comment lines that you can parse later.

#Creator:JohnSmith
#Date:....
#Columns:id,username,...
1,JohnSmith
2, ..
gaborsch
  • 15,408
  • 6
  • 37
  • 48
9

In case you are not sure that all possible readers/writers of the file will be able to interpret (and preserve) comments, create the tag in a second file using some name convention that links them. Example:

myCSVFile.csv
myCSVFile.csv.tag
Mario Rossi
  • 7,651
  • 27
  • 37