-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvtoData.py
50 lines (44 loc) · 1.71 KB
/
csvtoData.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
import csv
input = "mobile_app_user_dataset_1.csv"
output = "output.csv"
def is_empty_row(row):
second_number = row[1].strip()
return not second_number or not second_number.isdigit()
# Function to calculate the sum of ones and zeros in a row
def calculate_sum(row):
row_sum = 0
for cell in row[1:]:
cell = cell.strip()
if cell:
try:
if '.' in cell:
# Handle float values
row_sum += int(float(cell))
else:
row_sum += int(cell)
except ValueError:
# Handle non-numeric values
row_sum += 0
return row_sum
# Open the input and output files
with open(input, 'r') as input_file, open(output, 'w', newline='') as output_file:
csv_writer = csv.writer(output_file, quoting=csv.QUOTE_NONE, escapechar=' ')
linecount = 0
for line in input_file:
line = line.strip()
if linecount < 3 or not line:
linecount += 1
else:
# Split the line by commas to get the row
row = line.split(',')
if not is_empty_row(row):
row_sum = calculate_sum(row)
if row_sum % 2 == 0:
# If the sum is even, append '0' to the end
rest_of_text = ",".join([row[0]] + [cell if cell.strip() else "0" for cell in row[1:]] + ['0'])
else:
# If the sum is odd, append '1' to the end
rest_of_text = ",".join([row[0]] + [cell if cell.strip() else "0" for cell in row[1:]] + ['1'])
csv_writer.writerow([rest_of_text])
linecount += 1
print(f"Data extracted and saved to {output}")