[*]Сканировать CSV с помощью ленивого фрейма данных Polars
[*]Формат номер телефона с помощью функции
[*]Удалить нули и дубликаты
[*]Записать CSV в новый файл
Вот мой код
Код: Выделить всё
import sys
import json
import polars as pl
import phonenumbers
#define the variable and parse the encoded json
args = json.loads(sys.argv[1])
#format phone number as E164
def parse_phone_number(phone_number):
try:
return phonenumbers.format_number(phonenumbers.parse(phone_number, "US"), phonenumbers.PhoneNumberFormat.E164)
except phonenumbers.NumberParseException:
pass
return None
#scan the csv file do some filter and modify the data and then write the output to a new csv file
pl.scan_csv(args['path'], separator=args['delimiter']).select(
[args['column']]
).with_columns(
#convert the int phne number as string and apply the parse_phone_number function
pl.col(args['column']).cast(pl.String).map_elements(parse_phone_number).alias(args['column']),
#add another column list_id with value 100
pl.lit(args['list_id']).alias("list_id")
).filter(
#filter nulls
pl.col(args['column']).is_not_null()
).unique(keep="last").collect().write_csv(args['saved_path'], separator=",")
< п>Это нормально? Могу ли я оптимизировать производительность (хотя бы использование памяти)?
Я новичок в Polars, работаю с PHP и очень нуб в Python, так что извините, если мой код выглядит немного глупо, ха-ха.
Подробнее здесь: https://stackoverflow.com/questions/753 ... onger-time