0

我正在将一堆数据写入一个 excel 文件,到目前为止,我已经将所有数据导出到从第 1 行到第 36 行的每一行。每行包含 15 列数据,如下面的代码所示。我需要以某种方式将循环中第一个城市的数据发送到第 1 行,将第二个城市的数据发送到第 3 行,将第三个城市的数据发送到第 6 行,等等......我不能做类似的事情

row = row + 2

因为它们需要分配给预定的不均匀分布的行。

这是将数据发送到 excel 的脚本:

#!/usr/bin/env python

from xlutils.copy import copy
from xlrd import open_workbook

import canada

cities = canada.getCities()

for c in cities :
        c.retrieveTemps()


## 
# writing to excel
##
file_name = 'fcst_hilo_TEST.xls'
new_file_name = 'fcst_hilo.xls'
row = 1


column_names = ["high0", "low1", "high1", "low2", "high2",
                "low3", "high3", "low4", "high4", "low5",
                "high5", "low6", "high6", "low7", "high7"]

workbook_file = None
try :
        # currently xlwt does not implement this option for xslx files
        workbook_file = open_workbook(file_name, formatting_info=True)
except :
        workbook_file = open_workbook(file_name)

workbook = copy(workbook_file)
sheet = workbook.get_sheet(0)

for row, city in enumerate(cities, start=1):
    for column, col_name in enumerate(column_names, start=2):
        sheet.write(row, column, getattr(city, col_name))

workbook.save(new_file_name)

我有 36 个城市都列在一个单独的脚本中(如下)这是“加拿大”的一部分,导入到上面的脚本中:

class city(object):
    def __init__(self, city_name, link) :
        self.name = city_name
        self.url = link
        self.high0 = 0
        self.high1 = 0
        self.high2 = 0
        self.high3 = 0
        self.high4 = 0
        self.high5 = 0
        self.high6 = 0
        self.high7 = 0
        self.low1 = 0
        self.low2 = 0
        self.low3 = 0
        self.low4 = 0
        self.low5 = 0
        self.low6 = 0
        self.low7 = 0

    def retrieveTemps(self) :
        filehandle = urllib.urlopen(self.url)

        # get lines from result into array
        lines = filehandle.readlines()
        filehandle.close()

        # (for each) loop through each line in lines
        for line_number, line in enumerate(lines, start=1):
            # find string, position otherwise position is -1
            position0 = line.rfind('title="{}"'.format(date.strftime("%A")))
            position1 = line.rfind('title="{}"'.format(date1.strftime("%A")))
            position2 = line.rfind('title="{}"'.format(date2.strftime("%A")))
            position3 = line.rfind('title="{}"'.format(date3.strftime("%A")))
            position4 = line.rfind('title="{}"'.format(date4.strftime("%A")))
            position5 = line.rfind('title="{}"'.format(date5.strftime("%A")))
            position6 = line.rfind('title="{}"'.format(date6.strftime("%A")))

            if position0 > 0 :
                self.high0 = lines[line_number + 4].split('&')[0].split('>')[-1]
                self.low1 = lines[line_number + 18].split('&')[0].split('>')[-1]

            if position1 > 0 :
                self.high1 = lines[line_number + 4].split('&')[0].split('>')[-1]
                self.low2 = lines[line_number + 19].split('&')[0].split('>')[-1]

            if position2 > 0 :
                self.high2 = lines[line_number + 4].split('&')[0].split('>')[-1]
                self.low3 = lines[line_number + 19].split('&')[0].split('>')[-1]

            if position3 > 0 :
                self.high3 = lines[line_number + 4].split('&')[0].split('>')[-1]
                self.low4 = lines[line_number + 19].split('&')[0].split('>')[-1]

            if position4 > 0 :
                self.high4 = lines[line_number + 4].split('&')[0].split('>')[-1]
                self.low5 = lines[line_number + 19].split('&')[0].split('>')[-1]

            if position5 > 0 :
                self.high5 = lines[line_number + 4].split('&')[0].split('>')[-1]
                self.low6 = lines[line_number + 19].split('&')[0].split('>')[-1]
                self.low7 = lines[line_number + 19].split('&')[0].split('>')[-1]

            if position6 > 0 :
                self.high6 = lines[line_number + 4].split('&')[0].split('>')[-1]
                self.high7 = lines[line_number + 4].split('&')[0].split('>')[-1]
                break # done with loop, break out of it

    def getCities():
        return [
            #BRITISH COLUMBIA CITIES
            city('Prince George', 'http://www.weatheroffice.gc.ca/city/pages/bc-79_metric_e.html'),
            city('Kamloops', 'http://www.weatheroffice.gc.ca/city/pages/bc-45_metric_e.html'),
            city('Blue River', 'http://www.weatheroffice.gc.ca/city/pages/bc-22_metric_e.html'),

            # Alberta
            city('High Level', 'http://www.weatheroffice.gc.ca/city/pages/ab-24_metric_e.html'),
            city('Peace River', 'http://www.weatheroffice.gc.ca/city/pages/ab-25_metric_e.html'),
            city('Jasper', 'http://www.weatheroffice.gc.ca/city/pages/ab-70_metric_e.html'),
            city('Edmonton', 'http://www.weatheroffice.gc.ca/city/pages/ab-50_metric_e.html'),
            city('Calgary', 'http://www.weatheroffice.gc.ca/city/pages/ab-52_metric_e.html'),

            #SASKATCHEWAN CITIES

            city('Biggar', 'http://www.weatheroffice.gc.ca/city/pages/sk-2_metric_e.html'),
            city('Saskatoon', 'http://www.weatheroffice.gc.ca/city/pages/sk-40_metric_e.html'),
            city('Melville', 'http://www.weatheroffice.gc.ca/city/pages/sk-8_metric_e.html'),
            city('Canora', 'http://www.weatheroffice.gc.ca/city/pages/sk-3_metric_e.html'),
            city('Yorkton', 'http://www.weatheroffice.gc.ca/city/pages/sk-33_metric_e.html'),

            #MANITOBA CITIES

            city('Winnipeg', 'http://www.weatheroffice.gc.ca/city/pages/mb-38_metric_e.html'),
            city('Sprague', 'http://www.weatheroffice.gc.ca/city/pages/mb-23_metric_e.html'),

            #ONTARIO CITIES

            city('Thunder Bay', 'http://www.weatheroffice.gc.ca/city/pages/on-100_metric_e.html'),
            city('Sioux Lookout', 'http://www.weatheroffice.gc.ca/city/pages/on-135_metric_e.html'),
            city('Armstrong', 'http://www.weatheroffice.gc.ca/city/pages/on-111_metric_e.html'),
            city('Hornepayne', 'http://www.weatheroffice.gc.ca/city/pages/on-78_metric_e.html'),
            city('Sudbury', 'http://www.weatheroffice.gc.ca/city/pages/on-40_metric_e.html'),
            city('South Parry', 'http://www.weatheroffice.gc.ca/city/pages/on-103_metric_e.html'),
            city('Toronto', 'http://www.weatheroffice.gc.ca/city/pages/on-143_metric_e.html'),
            city('Kingston', 'http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html'),
            city('Cornwall', 'http://www.weatheroffice.gc.ca/city/pages/on-152_metric_e.html'),
            city('Sarnia', 'http://www.weatheroffice.gc.ca/city/pages/on-147_metric_e.html'),

            #QUEBEC CITIES

            city('Montreal', 'http://www.weatheroffice.gc.ca/city/pages/qc-147_metric_e.html'),
            city('Quebec', 'http://www.weatheroffice.gc.ca/city/pages/qc-133_metric_e.html'),
            city('La Tuque', 'http://www.weatheroffice.gc.ca/city/pages/qc-154_metric_e.html'),
            city('Saguenay', 'http://www.weatheroffice.gc.ca/city/pages/qc-166_metric_e.html'),
            city('Riviere-du-loup', 'http://www.weatheroffice.gc.ca/city/pages/qc-108_metric_e.html'),

            #NOVA SCOTIA CITIES

            city('Truro', 'http://www.weatheroffice.gc.ca/city/pages/ns-25_metric_e.html'),
            city('Halifax', 'http://www.weatheroffice.gc.ca/city/pages/ns-19_metric_e.html'),

            #NEW BRUNSWICK CITIES

            city('Edmundston', 'http://www.weatheroffice.gc.ca/city/pages/nb-32_metric_e.html'),
            city('Moncton', 'http://www.weatheroffice.gc.ca/city/pages/nb-36_metric_e.html'),
        ]

任何建议,将不胜感激。谢谢!

4

0 回答 0