You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1003 B
36 lines
1003 B
import datetime |
|
import csv |
|
from io import StringIO |
|
from django_redis import get_redis_connection |
|
|
|
|
|
# 清除redis锁 |
|
def clear_lock_func(): |
|
redis_conn = get_redis_connection() |
|
redis_key = f'ai_show{datetime.date.today().strftime("%Y%m%d")}' |
|
if redis_conn.exists(redis_key): |
|
redis_conn.delete(redis_key) |
|
|
|
|
|
# 将csv转化成字典 |
|
def scv_to_obj(csv_data): |
|
f = StringIO(csv_data) |
|
reader = csv.DictReader(f) |
|
return list(reader) |
|
|
|
|
|
# 列表转csv |
|
def list_to_csv(rows, include_header=True): |
|
""" |
|
将嵌套列表转换为 CSV 字符串 |
|
:param rows: 输入数据,格式为 [['col1','col2'], ['val1','val2'], ...] |
|
:param include_header: 若为 True,则 rows[0] 作为表头;否则全部当作数据行 |
|
""" |
|
output = StringIO() |
|
writer = csv.writer(output, lineterminator='\r\n') |
|
if include_header and rows: |
|
writer.writerow(rows[0]) |
|
writer.writerows(rows[1:]) |
|
else: |
|
writer.writerows(rows) |
|
return output.getvalue()
|
|
|