从两个XLXS单元剥离数据到csv

时间:2017-05-16 16:10:09

标签: python excel python-2.7 csv

我有一个问题,我试图从excel电子表格中的两个单元格中获取数据并将它们放入csv文件中。数据是lat和lon坐标,因此它们必须并排才能被程序读取。这就是我所拥有的:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xlwt
import xlrd
import csv
import os, openpyxl, glob
from openpyxl import Workbook


with open ('test.csv', 'wb') as csvfile:
  spamwriter = csv.writer(csvfile, delimiter=',')

  for file in glob.glob ("/test"):
    wb = openpyxl.load_workbook('test-data.xlsx')
    ws = wb.active
    def lat():
      for row in ws.iter_rows('Q2:Q65536'):
        for cell in row:
          lat = cell.value
          return lat
    def lon():
      for row in ws.iter_rows('R2:R65536'):
        for cell in row:
          lon = cell.value
          return lon  

  cord=lat()+","+lon()
  print (lat()+","+lon()) #just to see if its working
  #spamwriter.writerow([cord]) uncomment to write to file

然而它只给我第一行数据而不是其余行(测试数据有大约1500行)。我将如何完成整个文件?

1 个答案:

答案 0 :(得分:0)

这可能不是最动态的方式,但我会将pandas用于此任务。它内置了pd.read_excel()pd.to_csv()函数。

import pandas as pd
import string

latColumn = string.lowercase.index('q') # determine index that corresponds to Excel Column letter (user lower case)
longColumn = string.lowercase.index('r') # Does not work for AA, BB, ...

data = pd.read_excel('test-data.xlsx', 'Sheet1', parse_cols=[latColumn,longColumn])

# Total number of rows being read in 65536 - 2 = 65534

csvOut = "foo.csv"
data[:65534].to_csv(csvOut, index=False, header=False)

如果您需要附加到文件而不是替换它,请将data[:65534].to_csv(....)更改为

open(csvOut, 'a') as f:  #append to the .csv file of your likings
     data[:65534].to_csv(f, index=False, header=False)