0

こんにちは、以下のような構造の Excel ファイルがあります。

      Location       column2    column3
1     South Africa
2     
3     
4     
5     England
6     
7     
8     
9     U.S
10    
11    
12    

各場所の間のスペースを前の場所の名前で埋めることができるpythonスクリプトを作成しようとしています(つまり、場所として南アフリカで2から4までのスペースを埋め、6-8はで埋められます場所としてのイングランドなど)

誰かが私を正しい方向に向けることができれば幸いです.Thanks

4

2 に答える 2

1

わかりました、答えは私が作成したこのばかげたラッパーだと思いますxlrd(または、あなたが自分で書いたものです!)。重要なのは、関数が一度に 1 行ずつリストに読み込むことと、Python のリストが入力された順序を記憶していることです。ラッパーは、Excel シート名をそのシートの行のリストにマップする辞書を生成します (ここでは、シートごとに 1 つのテーブルを想定しています。それ以外の場合は、一般化する必要があります)。各行は、キーが列名であるディクショナリです。

あなたのために、私はあなたのデータを読み込んでから、次のようなことをします(テストされていません):

import see_below as sb
dict = sb.workbookToDict(your_file)
output = []
this_location = None
for row in dict[relevant_sheet_name]:
    output_row = row
    if row['Location'] is not None:
        this_location = row['Location']
    else:
        output_row['Location'] = this_location

リスト内包表記で何かかわいいことができるかもしれませんが、今夜はそれでだますにはワインが多すぎました:)

リーダーのラッパーは次のとおりです。

import xlrd


def _isEmpty(_):
        return ''


def _isString(element):
        return element.value.encode('ascii', 'ignore')


def _isFloat(element):
    return float(element.value)


def _isDate(element):
    import datetime
    rawDate = float(element.value)
    return (datetime.datetime(1899, 12, 30) +
            datetime.timedelta(days=rawDate))


def _isBool(element):
    return element.value == 1


def _isExcelGarbage(element):
    return int(element.value)


_options = {0: _isEmpty,
            1: _isString,
            2: _isFloat,
            3: _isDate,
            4: _isBool,
            5: _isExcelGarbage,
            6: _isEmpty}


def WorkbookToDict(filename):
    '''
        Reads .xlsx file into dictionary.

        The keys of the dictionary correspond to sheet names in the Excel workbook.
        The first row of the Excel workbook is taken to be column names, and each row
        of the worksheet is read into a separate dictionary, whose keys correspond to
        column names. The collection of dictionaries (as a list) forms the value in the
        dictionary. The output maps sheet names (keys) to a collection of dictionaries
        (value).
    '''
    book = xlrd.open_workbook(filename)
    allSheets = {}
    for s in book.sheets():
        thisSheet = []
        headings = [_options[x.ctype](x) for x in s.row(0)]

        for i in range(s.nrows):
            if i == 0:
                continue

            thisRow = s.row(i)
            if len(thisRow) != len(headings):
                raise Exception("Mismatch between headings and row length in ExcelReader")

            rowDict = {}
            for h, r in zip(headings, thisRow):
                rowDict[h] = _options[r.ctype](r)
            thisSheet.append(rowDict)
        allSheets[str(s.name)] = thisSheet
    return allSheets

ライターはこちら:

import xlwt

def write(workbookDict, colMap, filename):
    '''
       workbookDict should be a map of sheet names to a list of dictionaries.
       Each member of the list should be a mapping of column names to contents,
       missing keys are handled with the nullEntry field. colMap should be a
       dictionary whose keys are identical tto the sheet names in the workbookDict.
       Each value is a list of column names that are assumed to be in order.
       If a key exists in the workbookDict that does not exist in the colDict, the
       entry in workbookDict will not be written.
    '''

    workbook = xlwt.Workbook()

    for sheet in workbookDict.keys():
        worksheet = workbook.add_sheet(sheet)
        cols = colMap[sheet]
        i = 0
        writeCols = True
        while i <= len(workbookDict[sheet]):
            if writeCols:
                for j in range(len(cols)):
                    if writeCols:  # write col headings
                        worksheet.write(i, j, cols[j])
                writeCols = False
            else:
                for j in range(len(cols)):
                    worksheet.write(i, j, workbookDict[sheet][(i-1)][cols[j]])
            i += 1

    workbook.save(filename)

とにかく、これがうまくいくことを本当に願っています!

于 2013-01-29T03:11:37.937 に答える
1
wb = openpyxl.load_workbook('enter your workbook name')

sheet = wb.get_sheet_by_name('enter your sheet name')

row=sheet.max_row

for row in range (3,row):

   if sheet.cell(row=row, column=1).value is not None and sheet.cell(row=row+1,column=1).value is None:

        sheet.cell(row=row+1, column=1).value = sheet.cell(row=row, column=1).value

 wb.save('enter your workbook name')
于 2017-03-14T12:39:38.943 に答える