-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathis_csvreader.py
34 lines (28 loc) · 1.13 KB
/
is_csvreader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 17:55:11 2023
@author: Zoe
"""
import csv
def read_csv(filename):
"""Read a csv file and return it as a list of dictionaries:
where each list item is a row of the csv file"""
with open(f"Vocabulary/{filename}.csv", 'r', encoding = "utf-8") as infile:
return [x for x in csv.DictReader(infile)]
def rename_column(vocablist, oldname, newname):
"""Rename a column - note this changes the order of the columns,
but if this is a problem we can switch to using OrderedDict."""
for index, row in enumerate(vocablist):
vocablist[index][newname] = vocablist[index].pop(oldname)
def filter_matches(vocablist, colname, matchword):
"""Create a new vocab list where values in the
specified column match the specified matchword"""
return [x for x in vocablist if x[colname].lower() == matchword.lower()]
def filter_rows(vocablist, rowlist):
"""Filter the vocab list by row numbers"""
newlist = []
for x in rowlist:
newlist.append(vocablist[x])
return newlist
def getcol(vocablist, colname):
return [x[colname] for x in vocablist]