Я пытаюсь разработать некоторые файлы, используя SQLLITE, и записать информацию из этих файлов в отдельные столбцы в моей базе данных. Однако, наряду с ожидаемым числом, он также записывает в базу данных несколько пустых элементов
# establishing the baseline
conn = sqlite3.connect("C:\\Users\\winkl\\PycharmProjects\\data
science\\whatever.db")
c = conn.cursor()
c.execute("CREATE TABLE all_files (word count integer)")
files = [path1, path2, ...]
# gather the files
for file in files:
with open(file, "r") as csvfile:
filereader = csv.reader(csvfile, delimiter=",", quotechar="|",
quoting=csv.QUOTE_MINIMAL)
# make a column for each file in the table if there isn't one
try:
c.execute("ALTER TABLE all_files ADD COLUMN
{0}".format(str("book_" + str(files.index(file)))))
except:
pass
# just get the second line of every file, since this is only
# an experimental version
line_count = 0
for row in filereader:
if line_count == 0:
line_count = line_count + 1
elif line_count == 1:
# write the data to the database
c.execute("INSERT INTO all_files ({0}) VALUES
({1})".format(str("book_" + str(files.index(file))),
row[2]))
line_count = line_count + 1
# check result
c.execute("SELECT * FROM all_files")
print(c.fetchall())
Ожидаемый результат будет следующим:
[(0.84), (0.83), (0.76), (0.83), (0.81), (0.75)]
Вместо этого это так:
[(None, 0.84, None, None, None, None, None),
(None, None, 0.83, None, None, None, None),
(None, None, None, 0.76, None, None, None),
(None, None, None, None, 0.83, None, None),
(None, None, None, None, None, 0.81, None),
(None, None, None, None, None, None, 0.75)]
Почему?