-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_format.py
More file actions
68 lines (52 loc) · 2.38 KB
/
convert_format.py
File metadata and controls
68 lines (52 loc) · 2.38 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import pandas as pd
def convert_cricket_data(data, year, category):
# Split input text into lines
lines = data.strip().split("\n")
formatted_data = []
if category == "batting":
formatted_data = ["POS,Player,Mat,Inns,NO,Runs,HS,Avg,BF,SR,100,50,4s,6s,year"]
else:
formatted_data = ["POS,Player,Mat,Inns,Ov,Wkts,Runs,BBI,Avg,Econ,SR,4w,5w,year"]
i = 0
while i < len(lines):
if lines[i].strip().isdigit(): # Detect the rank number
rank = lines[i].strip()
player_name = lines[i + 2].strip()
team = lines[i + 3].strip()
stats = lines[i + 4].strip()
# Process the stats
stats_values = stats.split("\t")
print(stats_values)
# Ensure correct format (position, player + team, matches, innings, NO, runs, HS, avg, BF, SR, 100s, 50s, 4s, 6s, year)
formatted_line = f"{rank},{player_name}{team},{stats_values[1]},{stats_values[2]},{stats_values[3]},{stats_values[0]},{','.join(stats_values[4:])},{year}"
formatted_data.append(formatted_line)
i += 5 # Move to next entry
else:
i += 1
return "\n".join(formatted_data)
def process_file(input_file, output_file, year, category):
if os.path.exists(input_file):
with open(input_file, 'r', encoding='utf-8') as f:
data = f.read()
formatted_data = convert_cricket_data(data, year, category)
with open(output_file, 'w', encoding='utf-8') as f:
f.write(formatted_data)
print(f"Processed {input_file} -> {output_file}")
else:
print(f"File not found: {input_file}")
def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
year = 2025
input_files = {
"batting": os.path.join(script_dir, f"Data/{year}/Season_Data/batting_{year}.csv"),
"bowling": os.path.join(script_dir, f"Data/{year}/Season_Data/bowling_{year}.csv")
}
output_files = {
"batting": os.path.join(script_dir, f"Data/{year}/final_batting_{year}.csv"),
"bowling": os.path.join(script_dir, f"Data/{year}/final_bowling_{year}.csv")
}
for category in input_files:
process_file(input_files[category], output_files[category], year, category)
if __name__ == '__main__':
main()