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 5a12666

Browse files
CSV Column Length Analyzer script added.
1 parent 258cf2b commit 5a12666

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

‎CSV Column Length Analyzer/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# CSV Column Length Analyzer
2+
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.
4+
5+
## Requirements
6+
7+
- Python 3.x
8+
9+
## Usage
10+
11+
1. Ensure you have Python installed on your system.
12+
13+
2. Download the script file (`script.py`) to your local machine.
14+
15+
3. Open a terminal or command prompt and navigate to the directory where the script is located.
16+
17+
4. Modify the `FILE_PATH` global constant to specify the path to your CSV file.
18+
19+
```python
20+
FILE_PATH = 'file.csv'
21+
```
22+
23+
5. Run the script:
24+
25+
```bash
26+
python script.py
27+
```
28+
29+
6. The script will print the maximum length of each column in the following format:
30+
31+
```plaintext
32+
Column 1 Max Length: X
33+
Column 2 Max Length: Y
34+
Column 3 Max Length: Z
35+
...
36+
```

‎CSV Column Length Analyzer/script.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import csv
2+
import os
3+
4+
# Define global constants
5+
FILE_PATH = 'file.csv' # Replace with the actual path to your CSV file
6+
FILE_EXTENSION = '.csv'
7+
8+
def get_max_lengths(csv_file):
9+
max_lengths = {}
10+
11+
try:
12+
with open(csv_file, 'r', encoding='utf-8') as file:
13+
reader = csv.DictReader(file)
14+
15+
# Check if there is at least one column in the CSV file
16+
if not reader.fieldnames:
17+
raise ValueError("CSV file has no columns.")
18+
19+
# Initialize max_lengths dictionary with column names
20+
for column in reader.fieldnames:
21+
max_lengths[column] = 0
22+
23+
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]))
27+
28+
except FileNotFoundError:
29+
print(f"Error: File '{csv_file}' not found.")
30+
except Exception as e:
31+
print(f"Error: {e}")
32+
33+
return max_lengths
34+
35+
def main():
36+
file_path = FILE_PATH
37+
38+
# Check if the file has a .csv extension
39+
# if not file_path.lower().endswith(FILE_EXTENSION):
40+
# raise ValueError("Invalid file type. Please provide a CSV file.")
41+
42+
# Alternatively
43+
44+
# file_extension = os.path.splitext(file_path)[1]
45+
_, file_extension = os.path.splitext(file_path)
46+
if file_extension.lower() != FILE_EXTENSION:
47+
raise ValueError("Invalid file type. Please provide a CSV file.")
48+
49+
max_lengths = get_max_lengths(file_path)
50+
51+
if max_lengths:
52+
# Display the results
53+
for column, length in max_lengths.items():
54+
print(f'{column.capitalize()} Max Length: {length}')
55+
56+
if __name__ == "__main__":
57+
main()

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Python-Snippets-Hub
2-
A collection of small Python scripts for various tasks and utilities.
2+
3+
Welcome to the Python Snippets Hub! This repository is a collection of small Python scripts designed to tackle various tasks and utilities. Whether you're looking for quick solutions or inspiration for your projects, you'll find a variety of handy snippets here.

0 commit comments

Comments
(0)

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