in glam/api/management/commands/import_glean_aggs.py [0:0]
def sanitize_import_file_quickfix_1804769(self, fp_name):
"""Write and return a sanitized temp file without lines that contain invalid characters."""
out_fp_name = f"{fp_name}_sanitized"
with io.open(fp_name, "r", encoding="ascii") as f_in:
with io.open(out_fp_name, "w+", encoding="utf-8") as f_out:
for line in f_in:
try:
if b"\x00" not in line.encode("utf-8"):
f_out.write(line)
except UnicodeError:
# Skip the line if it is not UTF-8 compatible
continue
return out_fp_name