Search notes:

Python: poor man's CSV reader

The following script is a poor man's implementation of a CSV reader.
Of course, for serious work with CSV files, Python's standard library csv should be used.
#!/usr/bin/python3

csv = open('data.csv')

#
#  Skip header
#
next(csv)

print(' col_1 | col_2 | col_3')
print(' ------+-------+------')
for line in csv:

    line = line.rstrip()

  #
  # Remove the trailing new line
  #
    values=line.split(',')

    val_1 = values[0]
    val_2 = values[1]
    val_3 = values[2]

  #
  # Make empty values visually stand out
  #
    if val_1 == '':
       val_1 =  '-'
    if val_2 == '':
       val_2 =  '-'
    if val_3 == '':
       val_3 =  '-'

  #
  # Print the values read from the CSV file
  #
    print(' {:<5} | {:<5} | {:<5}'.format(val_1, val_2, val_3))
Github repository about-Python, path: /misc/read-csv/go.py

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758193700, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/code-snippets/read-csv(76): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78