使用Python打开多个excel文件
问题描述:
我在一个目录中有多个excel文件,并且希望一次打开这些文件以执行写入操作(例如,在所有excel文件的第一行中写入“Hi” )。 Python有没有办法做到这一点?使用Python打开多个excel文件
答
如果您需要更多的控制,你可以做这样的事情(从一个叫周杰伦,不知道的人解决办法,如果他是原作者。http://www.python-excel.org/)
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")
答
您可以使用:
import csv
with open('A.csv','w') as a, open ('B.csv', 'w') as b:
a_reader = csv.reader(a)
b_reader = csv.reader(b)
...
现在,你可以通过这两个文件进行迭代。
Python模块fileinput也有帮助,它允许迭代多个作为参数传递的文件的行。