Use csv.DictReader
:
import csv with open('test.csv') as f: a = [{k: int(v) for k, v in row.items()} for row in csv.DictReader(f, skipinitialspace=True)]
Will result in :
[{'col2': 2, 'col3': 3, 'col1': 1}, {'col2': 5, 'col3': 6, 'col1': 4}]
Related Posts:
- TypeError: list indices must be integers or slices, not str
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Converting Dictionary to List?
- TypeError: list indices must be integers or slices, not str
- In Python, when to use a Dictionary, List or Set?
- How can I get list of values from dict?
- Creating a dictionary from a csv file?
- Map to List error: Series object not callable
- How to return dictionary keys as a list in Python?
- How can I create an array/list of dictionaries in python?
- Writing a dictionary to a csv file with one line for every ‘key: value’
- Writing Python lists to columns in csv
- Create a list with initial capacity in Python
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Python – TypeError: ‘int’ object is not iterable
- Understanding slice notation
- Iterating over dictionaries using ‘for’ loops
- Understanding slice notation
- How do I sort a dictionary by value?
- How do I sort a dictionary by value?
- IndexError: list index out of range and python
- Python – TypeError: ‘int’ object is not iterable
- How do I sort a dictionary by value?
- Check if a given key already exists in a dictionary
- How to convert list to string [duplicate]
- Writing a pandas DataFrame to CSV file
- How do I get the number of elements in a list?
- How to overcome TypeError: unhashable type: ‘list’
- How does collections.defaultdict work?
- Finding the index of an item in a list
- Writing a pandas DataFrame to CSV file
- Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- How to make a flat list out of a list of lists
- Removing duplicates in lists
- What does the list() function do in Python?
- Is a Python dictionary an example of a hash table?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- TypeError: ‘float’ object is not subscriptable
- Converting dictionary to JSON
- Is there a short contains function for lists?
- How do I merge two dictionaries in a single expression (taking union of dictionaries)?
- TypeError: ‘float’ object is not subscriptable
- Transpose/Unzip Function (inverse of zip)?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- Check if something is (not) in a list in Python
- IndexError: too many indices for array
- Python list of dictionaries search
- Python: finding an element in a list [duplicate]
- Updating a dictionary in python
- unhashable type: ‘dict’ Type Error [duplicate]
- ValueError : I/O operation on closed file
- How to copy a dictionary and only edit the copy
- Finding the average of a list
- Writing a list to a file with Python
- How can I remove a key from a Python dictionary?
- How to solve dictionary changed size during iteration error?
- How do I concatenate two lists in Python?
- if else in a list comprehension
- How can I randomly select an item from a list?
- Print a list in reverse order with range()?
- DataFrame constructor not properly called
- Get unique values from a list in python [duplicate]
- Fastest way to check if a value exists in a list
- TypeError: ‘type’ object is not subscriptable when indexing in to a dictionary
- How can I count the occurrences of a list item?
- TypeError: ‘list’ object cannot be interpreted as an integer
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- How do I concatenate two lists in Python?
- if else in a list comprehension
- Finding median of list in Python
- Python set to list
- Python List vs. Array – when to use?
- TypeError: ‘dict’ object is not callable
- Pandas: ValueError: cannot convert float NaN to integer
- How to find all occurrences of an element in a list
- csv.Error: iterator should return strings, not bytes
- Getting key with maximum value in dictionary?
- Get list from pandas dataframe column or row?
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- Finding and replacing elements in a list
- How to find all the indexes of a recurring item in a list?
- Getting key with maximum value in dictionary?
- ValueError: max() arg is an empty sequence
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- Remove list from list in Python
- python – if not in list
- How do I concatenate two lists in Python?
- How can I compare two lists in python and return matches
- How can I get dictionary key as variable directly in Python (not by searching from value)?
- Are dictionaries ordered in Python 3.6+?
- Python – Reading and writing csv files with utf-8 encoding
- How can I remove a key from a Python dictionary?
- Replace values in list using Python