从csv文件中获取特定行,并使用python将其放入excel文件中
你好 我想从csv文件中获取一些特定的列(即第3列和第7列),这两列需要放入一个输出excel文件中 我认为我的问题是将数据放在excel文件中。 下面是我尝试使用的代码从csv文件中获取特定行,并使用python将其放入excel文件中,python,excel,csv,Python,Excel,Csv,你好 我想从csv文件中获取一些特定的列(即第3列和第7列),这两列需要放入一个输出excel文件中 我认为我的问题是将数据放在excel文件中。 下面是我尝试使用的代码 import csv from openpyxl import * from openpyxl.cell import get_column_letter file1=open('WebShop_Export_2016_Feb_19.csv','r') readfile=csv.reader(file1,delimiter=
import csv
from openpyxl import *
from openpyxl.cell import get_column_letter
file1=open('WebShop_Export_2016_Feb_19.csv','r')
readfile=csv.reader(file1,delimiter=';')
data=[]
wb=Workbook()
dest_filename = 'Name of OUPUT excel.xlsx'
ws = wb.active
ws.title="Output XLSX"
for row in readfile:
data=row[2],row[6] # take column 3 and 7 from source file
print(data)# this line is just to see the info that will be put in the excel output
for row_index, item in enumerate(data):
for column_index, cell in enumerate(item):
column_letter = get_column_letter((column_index + 1))
ws.cell('%s%s'%(column_letter, (row_index + 1))).value = cell
file1.close()
wb.save('Name of OUPUT excel.xlsx')
提前谢谢你 您需要将每一行的内容(例如,
[row[2],row[6]]
)附加到数据
——当您将其存储在数据
中时,即覆盖之前的内容,以便在您尝试将值放入excel文件时,数据中只有一个条目。输出一个两列的CSV文件可能更容易,Excel会很高兴地打开它。。看你!