Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4504e54

Browse files
Update CSV analysis script for improved insights and formatting.
1 parent 5a12666 commit 4504e54

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

‎CSV Column Length Analyzer/README.md renamed to ‎CSV Inspector/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# CSV Column Length Analyzer
1+
# CSV Inspector
22

3-
This simple Python script analyzes a CSV file and determines the maximum length of each column. The script uses the `csv` module to read the CSV file and iterates through each row to find the maximum length for each column.
3+
This Python script analyzes a CSV file using the csv module, iterating through rows to gather insights such as the total number of records, lengths, and count of empty cells in each column.
44

55
## Requirements
66

@@ -26,11 +26,16 @@ This simple Python script analyzes a CSV file and determines the maximum length
2626
python script.py
2727
```
2828

29-
6. The script will print the maximum length of each column in the following format:
29+
6. The script will display insights about each column in the following format:
3030

3131
```plaintext
32-
Column 1 Max Length: X
33-
Column 2 Max Length: Y
34-
Column 3 Max Length: Z
32+
Total Records: X
33+
Column 1 Max Length: Y
34+
Column 2 Max Length: Z
35+
Column 3 Max Length: W
36+
...
37+
Column 1 Empty Cells: A
38+
Column 2 Empty Cells: B
39+
Column 3 Empty Cells: C
3540
...
3641
```

‎CSV Column Length Analyzer/script.py renamed to ‎CSV Inspector/script.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
FILE_PATH = 'file.csv' # Replace with the actual path to your CSV file
66
FILE_EXTENSION = '.csv'
77

8-
def get_max_lengths(csv_file):
9-
max_lengths = {}
8+
def get_file_info(csv_file):
9+
file_info = {}
1010

1111
try:
1212
with open(csv_file, 'r', encoding='utf-8') as file:
@@ -16,21 +16,32 @@ def get_max_lengths(csv_file):
1616
if not reader.fieldnames:
1717
raise ValueError("CSV file has no columns.")
1818

19-
# Initialize max_lengths dictionary with column names
19+
# Initialize file_info dictionary with column names
2020
for column in reader.fieldnames:
21-
max_lengths[column] = 0
21+
file_info[column] = {
22+
'max_length': 0,
23+
'empty_cells': 0,
24+
}
25+
26+
total_records = 0
2227

2328
for row in reader:
24-
# Iterate through each column and update the max length if needed
25-
for column in max_lengths.keys():
26-
max_lengths[column] = max(max_lengths[column], len(row[column]))
29+
total_records += 1
30+
# Iterate through each column and update the variables
31+
for column in file_info.keys():
32+
cell_value = row[column]
33+
file_info[column]['max_length'] = max(file_info[column]['max_length'], len(str(cell_value)))
34+
35+
# Check for empty cells
36+
if not cell_value.strip():
37+
file_info[column]['empty_cells'] += 1
2738

2839
except FileNotFoundError:
2940
print(f"Error: File '{csv_file}' not found.")
3041
except Exception as e:
3142
print(f"Error: {e}")
3243

33-
return max_lengths
44+
return file_info, total_records
3445

3546
def main():
3647
file_path = FILE_PATH
@@ -46,12 +57,17 @@ def main():
4657
if file_extension.lower() != FILE_EXTENSION:
4758
raise ValueError("Invalid file type. Please provide a CSV file.")
4859

49-
max_lengths = get_max_lengths(file_path)
60+
file_info, total_records = get_file_info(file_path)
61+
5062

51-
if max_lengths:
63+
if file_info:
5264
# Display the results
53-
for column, length in max_lengths.items():
54-
print(f'{column.capitalize()} Max Length: {length}')
65+
66+
print(f'Total Records: {total_records}')
67+
68+
for column, info in file_info.items():
69+
print(f'{column.capitalize()} Max Length: {info["max_length"]}')
70+
print(f'{column.capitalize()} Empty Cells: {info["empty_cells"]}')
5571

5672
if __name__ == "__main__":
5773
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /