4.3. CSV Non-Standard

4.3.1. Parsing Non-CSV Files

Parsing /etc/passwd file with csv.DictReader():

import csv


FILE = r'_temporary.txt'
# root:x:0:0:root:/root:/bin/bash
# watney:x:1000:1000:Mark Watney:/home/watney:/bin/bash
# lewis:x:1001:1001:José Jiménez:/home/lewis:/bin/bash
# twardowski:x:1002:1002:Jan Twardowski:/home/twardowski:/bin/bash

with open(FILE) as file:
    result = csv.DictReader(
        file,
        fieldnames=['username', 'password', 'uid', 'gid', 'fullname', 'home', 'shell'],
        delimiter=':',
        lineterminator='\n',
        quoting=csv.QUOTE_NONE)

    for line in result:
        print(line)

# {'username': 'root', 'password': 'x', 'uid': '0',...}
# {'username': 'watney', 'password': 'x', 'uid': '1000',...}
# {'username': 'lewis', 'password': 'x', 'uid': '1001',...}
# {'username': 'twardowski', 'password': 'x', 'uid': '1002',...}

Parsing Java properties file with csv.DictReader():

import csv


FILE = r'_temporary.properties'
# sonar.projectKey=habitatOS
# sonar.projectName=habitatOS
# sonar.language=py
# sonar.sourceEncoding=UTF-8
# sonar.verbose=true

with open(FILE) as file:
    result = csv.DictReader(
        file,
        fieldnames=['property', 'value'],
        delimiter='=',
        lineterminator='\n',
        quoting=csv.QUOTE_NONE)

    for line in result:
        print(line)

# {'property': 'sonar.projectKey', 'value': 'habitatOS'}
# {'property': 'sonar.projectName', 'value': 'habitatOS'}
# {'property': 'sonar.language', 'value': 'py'}
# {'property': 'sonar.sourceEncoding', 'value': 'UTF-8'}
# {'property': 'sonar.verbose', 'value': 'true'}

4.3.2. Assignments