format date - python to cassandra

 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
def convert_date(str_date):
d = str_date.replace('.',' ').replace(':', ' ').replace('-', ' ').split(' ')
#print("d.len="+ str(len(d)))
f = ''
if len(d) == 5:
f = '%d.%m.%Y %H:%M'
elif len(d) == 6:
f = '%d.%m.%Y %H:%M:%S'
return datetime.strptime(str_date, f)
def format_date_for_import(date):
whole_date = date.split(" ")
d = whole_date[0].split(".")
return d[2]+"-"+d[1]+"-"+d[0]+" "+whole_date[1]
#before COPY count_calls.calls_table2 (num1, started, num2) FROM 'output_csv';
def format_csv_data_for_import(source, output_csv):
#needed: 79147349111,"{'2013-03-02 08:21:11+0400': '79140656269'}"
#needed: 79147349111,2013-03-02 08:21:11+0400,"79140656269"
data = open(source)
op = open(output_csv, 'w')
i = 0
for line in data:
if i < 1000:
i += 1
el = line.split(";")
if len(el) > 2 and len(el[2]) > 2:
date = format_date_for_import(el[1])
#print(date)
#string_to_write = el[0] + ",\"{'" + date + "': '" + el[2].split("\r\n")[0] + "'}\""
op.write(el[0] + ","+date + ",\"" + el[2].split("\r\n")[0] + "\"" + "\n")
op.write(el[2].split("\r\n")[0] + ","+date + ",\"" + el[0] + "\"" + "\n")
else:
break