|
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() |
|
|
|
|
|
with open(input_file_path, mode='r', encoding='utf-8') as input_file: |
|
reader = csv.DictReader(input_file) |
|
|
|
headers = [clean_column_name(header) for header in reader.fieldnames[:107]] |
|
|
|
state_files = {} |
|
|
|
for row in reader: |
|
state = row['Provider Business Mailing Address State Name'] |
|
|
|
|
|
if not re.match(r'^[A-Z]{2}$', state, re.I): |
|
continue |
|
|
|
|
|
file_name = f'{state}.csv' |
|
|
|
|
|
if state not in state_files: |
|
|
|
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: |
|
|
|
writer = csv.DictWriter(state_files[state], fieldnames=headers) |
|
|
|
|
|
cleaned_row = {clean_column_name(k): v for k, v in list(row.items())[:50]} |
|
writer.writerow(cleaned_row) |
|
|
|
|
|
for file in state_files.values(): |
|
file.close() |
|
|
|
if __name__ == "__main__": |
|
input_file_path = 'npidata_pfile_20050523-20240107.csv' |
|
process_file(input_file_path) |
|
|