在 excel 中写一个列表,其中每个元素都在一个单独的单元格中
Write a list in a excel where each element is in a seperate cell
我有一个图像数据集,并使用一个函数计算每个图像的哈希值,现在我有 128 位哈希值,结果如下:
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]
[0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]
...
现在我想把这个结果写在 excel sheet 中(每个数字在一个单独的单元格中),每行的最后一个单元格是每个图像的名称(例如“ ukbench00000.jpg”表示第一张图片,“ukbench00001.jpg”表示第二张图片,依此类推),
这是我在@Shagy G 的帮助下编写的代码:
from openpyxl import Workbook
book = Workbook()
sheet = book.active
folder_dir = "C:.../ExLets"
row = 1
for image in os.listdir(folder_dir):
binary_hash = compute_binary_hash(image)
_len = len(binary_hash)
for i in range(1, _len):
cell = sheet.cell(row=row, column=i)
cell.value = binary_hash[i]
cell = sheet.cell(row=row, column=_len+1)
cell.value = image
row += 1
column += 1
book.save('C:.../ExLets2.xlsx')
但我仍然缺少 binary_hash 的第一个数字,因为范围从 1 (而不是 0 )开始,以遵守行或列值必须至少为 1 的规则。
关于如何在 excel sheet 中写入 binary_hash 的所有数字,您有什么建议吗?
number_format = workbook.add_format({'num_format': '0'})
row = 1
for image in os.listdir(folder_dir):
binary_hash = compute_binary_hash(image)
_len = len(binary_hash)
for i in range(0, _len):
worksheet.write(row, i + 1, binary_hash[i], number_format)
worksheet.write(row, _len + 1, image)
row += 1
from openpyxl import Workbook
book = Workbook()
sheet = book.active
row = 1
for image in os.listdir(folder_dir):
binary_hash = compute_binary_hash(image)
_len = len(binary_hash)
for i in range(0, _len):
cell = sheet.cell(row=row, column=i + 1)
cell.value = binary_hash[i]
cell = sheet.cell(row=row, column=_len + 1)
cell.value = image
row += 1
book.save('images.xlsx')
使用pandas,您可以通过在行的每个单元格中插入数组来打开和编辑,如果您需要首先创建excel,运行第二个代码
import pandas as pd
df = pd.read_excel(f'YOUR_EXCEL.xlsx')
cols = df.columns
image_name = "ukbench00000.jpg"
binary_hash.append(image_name)
df.loc[len(df)] = binary_hash
df = df.set_index(f'{cols[0]}')
df.to_excel(f'YOUR_EXCEL.xlsx')
创建 EXCEL:
import pandas as pd
d = {}
df = pd.DataFrame(data = d,columns = [x for x in range(0, 128)])
df.to_excel(f'YOUR_EXCEL.xlsx')
我有一个图像数据集,并使用一个函数计算每个图像的哈希值,现在我有 128 位哈希值,结果如下:
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]
[0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]
...
现在我想把这个结果写在 excel sheet 中(每个数字在一个单独的单元格中),每行的最后一个单元格是每个图像的名称(例如“ ukbench00000.jpg”表示第一张图片,“ukbench00001.jpg”表示第二张图片,依此类推),
这是我在@Shagy G 的帮助下编写的代码:
from openpyxl import Workbook
book = Workbook()
sheet = book.active
folder_dir = "C:.../ExLets"
row = 1
for image in os.listdir(folder_dir):
binary_hash = compute_binary_hash(image)
_len = len(binary_hash)
for i in range(1, _len):
cell = sheet.cell(row=row, column=i)
cell.value = binary_hash[i]
cell = sheet.cell(row=row, column=_len+1)
cell.value = image
row += 1
column += 1
book.save('C:.../ExLets2.xlsx')
但我仍然缺少 binary_hash 的第一个数字,因为范围从 1 (而不是 0 )开始,以遵守行或列值必须至少为 1 的规则。
关于如何在 excel sheet 中写入 binary_hash 的所有数字,您有什么建议吗?
number_format = workbook.add_format({'num_format': '0'})
row = 1
for image in os.listdir(folder_dir):
binary_hash = compute_binary_hash(image)
_len = len(binary_hash)
for i in range(0, _len):
worksheet.write(row, i + 1, binary_hash[i], number_format)
worksheet.write(row, _len + 1, image)
row += 1
from openpyxl import Workbook
book = Workbook()
sheet = book.active
row = 1
for image in os.listdir(folder_dir):
binary_hash = compute_binary_hash(image)
_len = len(binary_hash)
for i in range(0, _len):
cell = sheet.cell(row=row, column=i + 1)
cell.value = binary_hash[i]
cell = sheet.cell(row=row, column=_len + 1)
cell.value = image
row += 1
book.save('images.xlsx')
使用pandas,您可以通过在行的每个单元格中插入数组来打开和编辑,如果您需要首先创建excel,运行第二个代码
import pandas as pd
df = pd.read_excel(f'YOUR_EXCEL.xlsx')
cols = df.columns
image_name = "ukbench00000.jpg"
binary_hash.append(image_name)
df.loc[len(df)] = binary_hash
df = df.set_index(f'{cols[0]}')
df.to_excel(f'YOUR_EXCEL.xlsx')
创建 EXCEL:
import pandas as pd
d = {}
df = pd.DataFrame(data = d,columns = [x for x in range(0, 128)])
df.to_excel(f'YOUR_EXCEL.xlsx')