prb_1/Импорт/import.py
2025-01-04 11:37:03 +05:00

32 lines
676 B
Python

from API.data.connect import init_db, Post, connect
def main():
init_db()
with open('users.csv', 'r', encoding='utf-8') as file:
data = file.read().split('\n')
resp = ''
for row in data:
splited_row = row.split(';')
role_name = splited_row[0]
if role_name == 'post_id':
continue
with connect() as session:
post = session.query(Post).filter(Post.title == role_name).first()
splited_row[0] = str(post.id)
resp += ';'.join(splited_row) + '\n'
with open('users.csv', 'w', encoding='utf-8') as file1:
file1.write(resp)
if __name__ == '__main__':
main()