I am having trouble figuring out the arguments to csv.dictreader and realized I have no clue what the square brackets signify.
From the docmentation:
class csv.DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])
I'd appreciate a summary of the arguments to the class instantiation.
Thanks
Square brackets indicate optional parts of a statement. They should not be entered. In many cases, items in the square brackets are optional because default values are provided. | A vertical bar indicates a choice between two or more items or values, usually within square brackets or curly braces.
The indexing operator (Python uses square brackets to enclose the index) selects a single character from a string. The characters are accessed by their position or index value.
mvBASIC Syntax NotationsAnything shown enclosed within square brackets is optional unless indicated otherwise. The square brackets themselves are not typed unless they are shown in bold. | A vertical bar that separates two or more elements indicates that any one of the elements can be typed.
The parentheses are used for passing arguments into a function.
The square brackets indicate that these arguments are optional. You can leave them out.
So, in this case you are only required to pass the csvfile
argument to csv.DictReader
. If you would pass a second parameter, it would be interpreted as the fieldnames
arguments. The third would be restkey
, etc.
If you only want to specify e.g. cvsfile
and dialect
, then you'll have to name the keyword argument explicitly, like so:
csv.DictReader(file('test.csv'), dialect='excel_tab')
For more on keyword arguments, see section 4.7.2 of the tutorial at python.org.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With