0

Googleスプレッドシートのデータに基づいて、家族のディレクトリエントリを作成するプログラムを作成しています。機能の1つは、家族の子供を誕生日とともに一覧表示することです。ただし、プログラムは、明確な理由なしに、誕生日の前にアポストロフィを印刷しています。たとえば、'01 / 01/01で、理由がわかりません。誕生日のデータを文字列に変換しましたが、他の文字列はこれを行っていません。なぜこうなった?

ソースコードの関連部分は次のとおりです。

    import gspread
    gc =gspread.login('mylogin','password')
    spreadsheet = gc.open_by_key("mykey")
    worksheet = spreadsheet.get_worksheet(0)
    Child1=(worksheet.cell(x,13)).value
    Child1BD=(worksheet.cell(x,14)).value
    Child2=(worksheet.cell(x,15)).value
    Child2BD=(worksheet.cell(x,16)).value
    Child3=(worksheet.cell(x,17)).value
    Child3BD=(worksheet.cell(x,18)).value
    Child4=(worksheet.cell(x,19)).value
    Child4BD=(worksheet.cell(x,20)).value
    # If there are no children, return without doing anything.
    if Child1 == "n/a":
        return;
    # If there is one child only, print the child's name and birthday.
    elif Child2 == "n/a" and Child1 != "n/a":
        print str(Child1)+"- "+str(Child1BD)
        return;
    # If thre are exactly two children, print their names and birthdays.
    elif Child3 == "n/a" and Child2 != "n/a":
        print str(Child1)+"- "+str(Child1BD)
        print str(Child2) +"- "+str(Child2BD)
        return;
    # If there are exactly three children, print their names and birthdays.
    elif Child4 == "n/a" and Child3 != "n/a":
        print str(Child1)+"- "+str(Child1BD)
        print str(Child2) +"- "+str(Child2BD)
        print str(Child3) +"- "+str(Child3BD)
        return;
    # If there are four children, print their names and birthdays.
    elif Child4 != "n/a":
        print str(Child1)+"- "+str(Child1BD)
        print str(Child2) +"- "+str(Child2BD)
        print str(Child3) +"- "+str(Child3BD)
        print str(Child4) +"- "+str(Child4BD)
        return;
4

2 に答える 2

4

これは、Excelが文字列を内部的に保存する方法です。印刷する前に削除する必要があります。

アポストロフィは、それが数字ではなく文字列であることを示します。

于 2013-03-07T03:35:40.590 に答える
1

'、セルのフォーマットを推測しようとするのを防ぐためにExcelで使用されます。たとえば、セルが自動的に日付に変換されないようにする場合は'01/01/2013

文字列がどこから来ているのかわからない場合は、'この魔法の(おそらく混乱する)再フォーマットを防ぐために、を前に付けることをお勧めします

マークが言うように、互換性を維持することはおそらくGoogleスプレッドシートでも同じです

一般に、表示された文字列を取得するために削除しても安全です'。エスケープされていない文字列をスプレッドシートに戻す場合は注意が必要です。

于 2013-03-07T03:48:11.517 に答える