File size: 2,486 Bytes
62dfb13 |
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 |
import streamlit as st
import csv
import os
import re
def delete_existing_state_csv_files():
"""Delete all CSV files with 6-character names in the current directory."""
for file in os.listdir('.'):
if re.match(r'^[A-Z]{2}\.csv$', file, re.I):
os.remove(file)
def clean_column_name(column_name):
"""Remove spaces and punctuation from column names, keeping only alphabets."""
return re.sub(r'[^a-zA-Z]', '', column_name)
def process_file(input_file_path):
# Delete existing state CSV files before processing
delete_existing_state_csv_files()
# Open the input file for reading
with open(input_file_path, mode='r', encoding='utf-8') as input_file:
reader = csv.DictReader(input_file)
# Prepare headers with cleaned column names and truncate to the first 107 fields
headers = [clean_column_name(header) for header in reader.fieldnames[:107]]
state_files = {} # Dictionary to keep track of open file handles for each state
for row in reader:
state = row['Provider Business Mailing Address State Name']
# Skip if state is not exactly two letters
if not re.match(r'^[A-Z]{2}$', state, re.I):
continue
# Generate the file name based on the state
file_name = f'{state}.csv'
# Check if we've already opened this state file
if state not in state_files:
# Open a new file for the state and write the header
state_file = open(file_name, mode='w', newline='', encoding='utf-8')
writer = csv.DictWriter(state_file, fieldnames=headers)
writer.writeheader()
state_files[state] = state_file
else:
# Get the writer for the already opened file
writer = csv.DictWriter(state_files[state], fieldnames=headers)
# Write the current row to the state's file, cleaning and truncating each row to the first 50 fields
cleaned_row = {clean_column_name(k): v for k, v in list(row.items())[:50]}
writer.writerow(cleaned_row)
# Close all open state files
for file in state_files.values():
file.close()
if __name__ == "__main__":
input_file_path = 'npidata_pfile_20050523-20240107.csv' # Replace with the path to your large file
process_file(input_file_path)
|