Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import ftfy
|
| 4 |
+
|
| 5 |
+
def fix_complex_encoding(text):
|
| 6 |
+
try:
|
| 7 |
+
# First, try to fix text using ftfy
|
| 8 |
+
fixed_text = ftfy.fix_text(text)
|
| 9 |
+
# Additional encoding and decoding steps to handle more cases
|
| 10 |
+
fixed_text = (fixed_text.encode('latin1', errors='ignore')
|
| 11 |
+
.decode('utf-8', errors='ignore'))
|
| 12 |
+
return fixed_text
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return text
|
| 15 |
+
|
| 16 |
+
def process_dataframe(df):
|
| 17 |
+
return df.applymap(fix_complex_encoding)
|
| 18 |
+
|
| 19 |
+
st.title("CSV Encoding Fixer")
|
| 20 |
+
|
| 21 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
|
| 22 |
+
|
| 23 |
+
if uploaded_file is not None:
|
| 24 |
+
df = pd.read_csv(uploaded_file, dtype=str)
|
| 25 |
+
st.write("Original Data:")
|
| 26 |
+
st.dataframe(df)
|
| 27 |
+
|
| 28 |
+
processed_df = process_dataframe(df)
|
| 29 |
+
st.write("Processed Data:")
|
| 30 |
+
st.dataframe(processed_df)
|
| 31 |
+
|
| 32 |
+
output_csv = processed_df.to_csv(index=False).encode('utf-8')
|
| 33 |
+
st.download_button(
|
| 34 |
+
label="Download Processed CSV",
|
| 35 |
+
data=output_csv,
|
| 36 |
+
file_name='processed_file.csv',
|
| 37 |
+
mime='text/csv'
|
| 38 |
+
)
|