Найти - Пользователи
Полная версия: Нужно правильно записать в БД
Начало » Python для новичков » Нужно правильно записать в БД
1
y_starynets
На выходе получаю вот такой список, подскажите пожалуйста как можно 20190329 перенести на 1 позицию. То есть что бы было вот так ('20190329', ). И как сделать правильно запись в базу? Выдает ошибку not enough arguments for format string.
 [(['00:00:00', '445.011'], '20190329'), (['00:00:01', '965.649'], '20190329'), (['00:00:02', '461.963'], '20190329'), (['00:00:03', '492.741'], '20190329')

Код
 with open('20190329.txt', 'r') as f:
    content=[s.split() for s in f]
    #a, b=zip(*content)
    #print(a); print(b)
ccc = f.name
print(ccc)
ds = os.path.splitext(ccc)[0]
cd = list (zip(content, itertools.repeat(ds)))
print(cd)
bd = type(content)
print(bd)
conn = MySQLdb.connect()
cursor = conn.cursor()
stmt = "INSERT INTO Vaza (Time, Nom, Date) VALUES (%s, %s, %s)"
cursor.executemany(stmt, cd)
conn.commit()
conn.close()
JOHN_16
Вам надо что бы в cd был список списков вида
 [['00:00:00', '445.011', '20190329'], ... ]
JOHN_16
Хотя бы так
 ds = os.path.splitext(ccc)[0]
with open('20190329.txt', 'r') as f:
    content=[s.split() + [ds] for s in f]
FishHook
 data = [(['00:00:00', '445.011'], '20190329'), (['00:00:01', '965.649'], '20190329')]
data = [(x[1], x[0]) for x in data]
print(data)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB