2017-10-01 5 views
1

私は緑色の個体数を計算するプログラムを作成しました。私が苦労している課題は、いくつかの一連の変数をExcelにエクスポートすることです。私はgreenfliesの数学を試しましたが、スプレッドシートに入れる方法を理解できません。コードは次のとおりです。Excelスプレッドシートへの変数のエクスポート

import winsound 
import time 
winsound.PlaySound("SystemHand", winsound.SND_ALIAS) #startup 
#menu 
print("Greenfly population model") 
time.sleep(1) 
one=1 
print("1: Set the generation 0 values") 
time.sleep(1) 
two=2 
print("2: Display the generation 0 values") 
time.sleep(1) 
three=3 
print("3: Run the model") 
time.sleep(1) 
four=4 
print("4: Export the data") 
time.sleep(1) 
five=5 
print("5: Quit") 
time.sleep(1) 
#base code 
while True: #wont shut down till option 5 is entered 
    print("") 
    ans=int(input("Please enter the number of the choice you want ")) 

    if ans == one: 
     while True: 
      generations=float(input("Enter the number of generations you want the model to run for (Must be in 5 and 25 inclusive) ")) 
      if generations < 5 or generations > 25: 
       print("Between 5 and 25 please") 
      else: 
       break 
     while True: 
       adultsur = float(input("Choose adult survival rate between 0 and 1 ")) 
       if adultsur < 0 or adultsur > 1: 
        print ("Between 1 and 0 please") #wont mess up the decimals 
       else: 
        break 
     while True: 
      juvensur=float(input("Choose juvenile survivle rate between 0 and 1 ")) 
      if juvensur < 0 or juvensur > 1: 
       print ("Between 1 and 0 please") 
      else: 
       break 
     while True: 
      sensur=float(input("Choose senile survivle rate between 0 and 1 ")) 
      if sensur < 0 or sensur > 1: 
       print ("Between 1 and 0 please") 
      else: 
       break 
     juv=int(input("Choose the amount of juveniles ")) 
     adu=int(input("Choose the amount of adults ")) #had issue with floats here so left it as int(input()) 
     sen=int(input("Choose the amount of seniles ")) 
     birth=int(input("Enter the birthrate of the adults ")) 

    if ans == two: 
     print("The new generation to model is ",generations) #no issues here 
     time.sleep(1) 
     print("The adult survivul rate is ",adultsur) 
     time.sleep(1) 
     print("The juvenile survivul rate is ",juvensur) 
     time.sleep(1) 
     print("The senile survivul rate is ",sensur) 
     time.sleep(1) 
     print("There are ",juv," juveniles") 
     time.sleep(1) 
     print("There are ",adu," juveniles") 
     time.sleep(1) 
     print("There are ",sen," juveniles") 
     time.sleep(1) 
     print("The birthrate of adults is ",birth) 

    if ans == three: 
     print("Running module.") 
     time.sleep(0.1) 
     print("Running module..") 
     time.sleep(0.1) 
     print("Running module...") 
     time.sleep(0.1) 
     counter = 0 
     print("GENERATION JUVENILES ADULTS SENILES") 
     while generations > counter: 
      print ("",int(counter)," ", int(juv)," ", int(adu)," ", int(sen)) 
      int(sen) 
      int(adu)#gets rid off the neverending decimals and rounds it up 
      int(juv) 
      sen *= sensur  #takes out the old seniles 
      adu *= adultsur  #does the math in a much simpler way than basic maths 
      juv *= len 
      juvn = juv 
      juv = adu * birth 
      sen += adu 
      adu += juvn 
      counter= counter+1 #will only repeat how many times the code was set to run 

    if ans == four 
      print ("",int(counter)," ", int(juv)," ", int(adu)," ", int(sen)) 
      int(sen) 
      int(adu) 
      int(juv) 
      sen *= sensur 
      adu *= adultsur 
      juv *= len 
      juvn = juv 
      juv = adu * birth 
      sen += adu 
      adu += juvn 
      counter= counter+1 

    if ans == five: 
     print("Understandable, have a great day") 
     winsound.PlaySound("SystemExit", winsound.SND_ALIAS) 
     winsound.PlaySound("*", winsound.SND_ALIAS) 
     break 

エクスポートするか、私に教えてください。おかげさまで

+0

エクスポートされたファイルを '.xls'や' .xlsx'形式のような実際のExcelファイルにしたいのですか、 '.csv'(カンマ区切りの値)で十分ですか?後者は書き出しが簡単で、書式は含まれませんが、Excelに簡単にインポートできます。注:必要以上に多くのコードを表示します。問題を示すための最小限のコードを示してください。 –

答えて

0

Excelにデータを書き込む方法はいくつかありますが、さらに複数のフォーマットがあります。

Python csvモジュールは、表形式データの書き込みと読み取りに使用できるクラスを実装しています。ファイルへの書き込みにはcsv.writerを使用するか、csv.DictWriterを使用できます。

csv.writerは、データを区切り文字列に変換することを担当するwriterオブジェクトを返します。ここではそれを使用する方法の例です:

import csv 

data_header = ['id', 'width', 'height'] 
data = [[0, 10, 5], 
     [1, 2, 3], 
     [2, 50, 40]] 

with open('test.csv', 'w') as file_writer: 
    writer = csv.writer(file_writer) 

    writer.writerow(data_header) 
    for item in data: 
     writer.writerow(item) 

csv.DictWriterは正規のwriterのように動作する辞書を出力行にマップするオブジェクトを返します。ここではそれを使用する方法の例です:

import csv 

data_header = ['id', 'width', 'height'] 
data = [{'id': 0, 'width': 10, 'height': 5}, 
     {'id': 1, 'width': 2, 'height': 3}, 
     {'id': 2, 'width': 50, 'height': 40}] 

with open('test.csv', 'w') as file_writer: 
    dict_writer = csv.DictWriter(file_writer, data_header) 

    dict_writer.writeheader() 
    for item in data: 
     dict_writer.writerow(item) 

あなたが見ることができるように、csv.writercsv.DictWriterの違いは、あなたがインターフェイスにデータを提供する方法です。 csv.writerでは、リストにデータを提供し、正しい列に書き込まれるようにリスト内の順序を保持する責任があります。csv.DictWriterキーを列名とし、値はデータである辞書を提供します列のために。

関連する問題