Files
presidio/samples/python/process_csv_file.py
SharonHart 2f708d224d Fix hero mascot hidden/oversized on mobile
The hero mascot was hidden on phones (display:none breakpoint) and, once shown,
ballooned because the theme's .md-typeset img max-width:100% beat the mascot
rule. Scope the mobile mascot size with higher specificity (max-width:180px,
centered) so Inky renders cleanly on mobile. Desktop unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-28 16:18:06 +03:00

52 lines
1.6 KiB
Python

import csv
import pprint
from typing import List, Iterable, Optional
from presidio_analyzer import BatchAnalyzerEngine, DictAnalyzerResult
from presidio_anonymizer import BatchAnonymizerEngine
"""
Example implementing a CSV analyzer
This example shows how to use the Presidio Analyzer and Anonymizer
to detect and anonymize PII in a CSV file.
It uses the BatchAnalyzerEngine to analyze the CSV file, and
BatchAnonymizerEngine to anonymize the requested columns.
Content of csv file:
id,name,city,comments
1,John,New York,called him yesterday to confirm he requested to call back in 2 days
2,Jill,Los Angeles,accepted the offer license number AC432223
3,Jack,Chicago,need to call him at phone number 212-555-5555
"""
class CSVAnalyzer(BatchAnalyzerEngine):
def analyze_csv(
self,
csv_full_path: str,
language: str,
keys_to_skip: Optional[List[str]] = None,
**kwargs,
) -> Iterable[DictAnalyzerResult]:
with open(csv_full_path, 'r') as csv_file:
csv_list = list(csv.reader(csv_file))
csv_dict = {header: list(map(str, values)) for header, *values in zip(*csv_list)}
analyzer_results = self.analyze_dict(csv_dict, language, keys_to_skip)
return list(analyzer_results)
if __name__ == "__main__":
analyzer = CSVAnalyzer()
analyzer_results = analyzer.analyze_csv('./csv_sample_data/sample_data.csv',
language="en")
pprint.pprint(analyzer_results)
anonymizer = BatchAnonymizerEngine()
anonymized_results = anonymizer.anonymize_dict(analyzer_results)
pprint.pprint(anonymized_results)