Help:CSV format

From semantic-mediawiki.org
CSV format
Outputs results in Windows-style Comma Separated Value format.
Further Information
Provided by: Semantic MediaWiki
Added: 1.2.1
Removed: still supported
Requirements: none
Format name: csv
Enabled? Indicates whether the result format is enabled by default upon installation of the respective extension. yes
Authors: Nathan Yergler , Markus Krötzsch , James Hong Kong
Categories: export · plaintext
Table of Contents

The result format csv is used to produce links to CSV files which allow to export the result of a query.

Semantic MediaWiki 3.0.0Released on 11 October 2018 and compatible with MW 1.27.0 - 1.31.x. introduced a set of predefined classes supporting a datatable output12 which also allows for CSV export via result format "Table"Outputs the results in a table (default for queries with printout statements)..

Parameters[edit]

General[edit]

Parameter Type Default Description
source text empty Alternative query source
limit whole number 50 The maximum number of results to return
offset whole number 0 The offset of the first result
link text all Show values as links
sort list of texts empty Property to sort the query by
order list of texts empty Order of the query sort
headers text show Display the headers/property names
mainlabel text no The label to give to the main page name
intro text empty The text to display before the query results, if there are any
outro text empty The text to display after the query results, if there are any
searchlabel text CSV Text for continuing the search
default text empty The text to display if there are no query results

Format specific[edit]

Parameter Type Default Description
sep text , Specifies a column separator
valuesep text , Specifies a value separator
showsep yes/no no Show separator in top of CSV file ("sep=<value>")
filename text result.csv The name for the output file
merge yes/no no Merge rows and column values with an identical subject identifier (aka first column)
bom yes/no no Add a BOM (character to signal endianness) at the top of the output file
Semantic MediaWiki 3.0.0Released on 11 October 2018 and compatible with MW 1.27.0 - 1.31.x. brought the "valuesep"3, "merge"45 and "bom"6 parameters.

Remarks[edit]

  • All numeric or named entities in the generated output are decoded as UTF-8 strings. Therefore the character set in your application has to be set accordingly (UTF-8).
  • On most platforms, CSV files will be automatically opened by the default application for using spreadsheets.

Examples[edit]

Default[edit]

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=csv
}}
Result

CSV

Change file name[edit]

Another file name for exporting the results can be specified by changing the filename parameter:

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=csv
 |filename=example.csv
}}
Result

CSV

Change separators[edit]

Between results[edit]

Another separator between results than "," e.g. ";" can be used if specified by the sep parameter:

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]]
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=csv
 |sep=;
}}
Result

CSV

Best used in combination with outputting the separator.

Between property values[edit]

Another separator between results than "," e.g. "~" can be used if specified by the valuesep parameter:

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]]
 |?Population
 |?Area#km²=Area
 |?Has zoo=Zoo
 |mainlabel=City
 |format=csv
 |valuesep=~
}}
Result

CSV

Output separator used[edit]

The separator used between results can be listed too in the exported file by setting the showsep parameter to yes:

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]]
 |?Population
 |?Area#km²=Area
 |?Has zoo=Zoo
 |mainlabel=City
 |format=csv
 |showsep=yes
}}
Result

CSV

Best used if the separator between results was changed from the default value.

Combine results for identical subject identifiers[edit]

Subject identifier with identical row and column values can be merged by setting the merge parameter to yes, e.g. lines containing "Foo", "1", "2", "3" and "Foo", "4", "5", "6" will result in "Foo", "1,4", "2,5", "3,6".5. Such a situation can normally only appear when mainlabel=- is used that creates a specific first column and contains identical identifier.

Add the byte order mark[edit]

The byte order mark (BOM) can be added to the exported file by setting the bom parameter to yes:

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]]
 |?Population
 |?Area#km²=Area
 |?Has zoo=Zoo
 |mainlabel=City
 |format=csv
 |bom=yes
}}
Result

CSV

This is especially useful for Microsoft compilers and interpreters as well as many pieces of software on Microsoft Windows such as Notepad since they treat the BOM as a required magic number rather than use heuristics for interpreting the file content.




References

  1. ^  Semantic MediaWiki: GitHub pull request gh:smw:2420
  2. ^  Semantic MediaWiki: GitHub pull request gh:smw:2718
  3. ^  Semantic MediaWiki: GitHub pull request gh:smw:2826
  4. ^  Semantic MediaWiki: GitHub pull request gh:smw:2822
  5. a b  Semantic MediaWiki: Sandbox example sb:smw:2822
  6. ^  Semantic MediaWiki: GitHub pull request gh:smw:2824