Python

Using Python’s DictCursor in MySQL to Return a Dict with Keys

The default MySQLdb library in Python only returns an array of values in the order you retrieve them. Here is an example: While that does the job, being able to call the fields using employee[‘name’], employee[‘position’] and employee[‘age’] makes a lot more sense. Here is how to use Python’s MySQLdb’s cursor library to return a …

Using Python’s DictCursor in MySQL to Return a Dict with Keys Read More »

Reading and Writing CSV Files with Python DictReader and DictWriter

Python can be an extremely powerful tool for reading and writing csv files. Here I’ll demonstrate an example of each using the csv DictWriter function. Sample File: (test.csv) firstname,lastname,age,favoritecolor john,smith,24,red jane,doe,26,blue sophe,miller,33,yellow jason,doe,21,red From here, I will import the csv file into a dictionary array using the csv DictReader function in the python console. You …

Reading and Writing CSV Files with Python DictReader and DictWriter Read More »