openfree commited on
Commit
9faad6b
·
verified ·
1 Parent(s): 9abfad3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -14,20 +14,16 @@ def convert_parquet_to_jsonl(parquet_file=None, parquet_url=None):
14
  else:
15
  raise ValueError("Either parquet_file or parquet_url must be provided")
16
 
17
- # Function to clean any invalid UTF-8 sequences by replacing them
18
- def clean_string(val):
19
- if isinstance(val, str):
20
- # Re-encode string to UTF-8, replacing invalid characters
21
- return val.encode("utf-8", errors="replace").decode("utf-8", errors="replace")
22
- return val
23
 
24
- # Apply cleaning to every cell in the DataFrame
25
- df = df.applymap(clean_string)
26
-
27
- # Now safely convert to JSON Lines
28
  jsonl_data = df.to_json(orient="records", lines=True)
29
-
30
- # Write out to a file
31
  output_file_path = "output.jsonl"
32
  with open(output_file_path, "w", encoding="utf-8") as f:
33
  f.write(jsonl_data)
 
14
  else:
15
  raise ValueError("Either parquet_file or parquet_url must be provided")
16
 
17
+ # Clean string columns to replace invalid UTF-8 sequences
18
+ for col in df.select_dtypes(include=["object"]).columns:
19
+ df[col] = df[col].apply(
20
+ lambda x: x.encode("utf-8", errors="replace").decode("utf-8", errors="replace") if isinstance(x, str) else x
21
+ )
 
22
 
23
+ # Convert to JSON Lines
 
 
 
24
  jsonl_data = df.to_json(orient="records", lines=True)
25
+
26
+ # Write the output to a file using UTF-8 encoding explicitly
27
  output_file_path = "output.jsonl"
28
  with open(output_file_path, "w", encoding="utf-8") as f:
29
  f.write(jsonl_data)