-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathciperpus_util.py
53 lines (41 loc) · 1.35 KB
/
ciperpus_util.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import itertools
from allpairspy import AllPairs
from csv import reader, DictReader
import os
import random
def list_to_string(list):
return "[%s]" % ", ".join([str(x) for x in list])
def combine_lists(lists):
return list(itertools.chain.from_iterable(lists))
def zip_lists(lists):
return list(zip(*lists))
def pair_lists(lists):
return AllPairs(lists)
#https://thispointer.com/python-read-csv-into-a-list-of-lists-or-tuples-or-dictionaries-import-csv-to-list/
def read_csv_tuples(path, remove_first=True):
# open file in read mode
with open(path, 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Get all rows of csv from csv_reader object as list of tuples
list_of_tuples = list(map(tuple, csv_reader))
if remove_first:
list_of_tuples.pop(0)
return list_of_tuples
def read_csv_dictionary(path):
# open file in read mode
with open(path, 'r') as read_obj:
# pass the file object to DictReader() to get the DictReader object
dict_reader = DictReader(read_obj)
# get a list of dictionaries from dct_reader
list_of_dict = list(dict_reader)
return list_of_dict
def read_payload(path):
lines = None
with open(path) as f:
lines = [line.rstrip('\n') for line in f]
random.shuffle(lines)
return lines
def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)