This shows you the differences between two versions of the page.
Next revision Both sides next revision | |||
docu:csheet:sysadm:script:python:read_csv [2020/07/08 08:02] admin created |
docu:csheet:sysadm:script:python:read_csv [2020/07/08 08:05] admin |
||
---|---|---|---|
Line 2: | Line 2: | ||
\\ | \\ | ||
- | In case we want to read a .csv file on python (3), and get a JSON (dictionary on python) with the first row defining the field names of each item, we can use this code snippet: | + | If you just want to **create your own code** from scratch, with the very **basic .csv reading code**, this is the snippet you need: |
+ | <code python> | ||
+ | def read_csv_data(fname): | ||
+ | with open(fname) as csvfile: | ||
+ | reader = csv.reader(csvfile, | ||
+ | for row in reader: | ||
+ | print(row) | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | In case we want to **read a .csv** file on python (3), and **get a JSON** (dictionary on python) with the **first row defining the field names** of each item, we can use this code snippet: | ||
<code python> | <code python> |