\

Pythonは、Excelファイルを操作するための様々なライブラリを提供しています。その中でも、xlwtpandasopenpyxlXlsxWriterなどがよく使われます。

xlwtを使った方法

import xlwt

x = 1
y = 2
z = 3
list1 = [2.34, 4.346, 4.234]

book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Sheet 1")

sheet1.write(0, 0, "Display")
sheet1.write(1, 0, "Dominance")
sheet1.write(2, 0, "Test")

sheet1.write(0, 1, x)
sheet1.write(1, 1, y)
sheet1.write(2, 1, z)

sheet1.write(4, 0, "Stimulus Time")
sheet1.write(4, 1, "Reaction Time")

i = 4
for n in list1:
    i = i + 1
    sheet1.write(i, 0, n)

book.save("trial.xls")

pandasを使った方法

from pandas import DataFrame

l1 = [1, 2, 3, 4]
l2 = [1, 2, 3, 4]
df = DataFrame({'Stimulus Time': l1, 'Reaction Time': l2})

df.to_excel('test.xlsx', sheet_name='sheet1', index=False)

これらのライブラリを使うことで、PythonからExcelファイルにデータを書き込むことが可能になります。それぞれのライブラリには独自の特性と機能がありますので、用途に応じて適切なものを選択してください。

投稿者 admin

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です