2014-10-30 93 views
0

閱讀此時此刻我寫了這個代碼:需要幫助編寫和使用Python

class device: 
    naam_device = '' 
    stroomverbuirk = 0 

aantal_devices = int(input("geef het aantal devices op: ")) 

i = aantal_devices 
x = 0 

voorwerp = {} 
while i > 0: 
    voorwerp[x] = device() 
    i = i - 1 
    x = x + 1 

i = 0 

while i < aantal_devices : 
    voorwerp[i].naam_device = input("Wat is device %d voor een device: " % (i+1)) 
    # hier moet nog gekeken worden naar afvang van foute invoer bijv. als gebruiker een string of char invoert ipv een float 
    voorwerp[i].stroomverbruik = float(input("hoeveel ampére is uw device?: ")) 
    i += 1 

i = 0 
totaal = 0.0 

##test while print 
while i < aantal_devices: 
    print(voorwerp[i].naam_device,voorwerp[i].stroomverbruik) 
    #dit totaal moet nog worden geschreven naar een bestand zodat je na 256 invoeren een totaal kan bepalen. 
    totaal = totaal + voorwerp[i].stroomverbruik 
    i = i + 1 

print("totaal ampére = ",totaal) 


aantal_koelbox = int(input("Hoeveel koelboxen neemt u mee?: ")) 

if aantal_koelbox <= 2 or aantal_koelbox > aantal_devices: 

    if aantal_koelbox > aantal_devices: 
     toestaan = input("Deelt u de overige koelboxen met mede-deelnemers (ja/nee)?: ") 
     if toestaan == "ja": 
      print("Uw gegevens worden opgeslagen! u bent succesvol geregistreerd.") 
     if toestaan == "nee": 
      print("Uw gegevens worden niet opgeslagen! u voldoet niet aan de eisen.") 
else: 
    print("Uw gegevens worden niet opgeslagen! u voldoet niet aan de eisen.") 

現在我想的totaal值寫入文件,後來當我保存這些投入256我要寫另一個程序來讀取256個輸入,並給出這些數字的總和,然後將這個數字除以14.如果有人能夠幫助我在正確的軌道上寫入數值並稍後讀取它們,我可以嘗試瞭解如何完成最後一部分。

但我已經嘗試了2天,現在仍然沒有找到好的解決方案來編寫和閱讀。

+1

https://docs.python.org/2/tutorial/inputoutput。html#閱讀和寫作文件 – MattDMo 2014-10-30 20:18:35

+1

你能舉一個你的嘗試的例子,大概是最好的嗎? – 2014-10-30 20:18:49

+0

此外,不要問如何做到這兩個部分,只要專注於讓寫作工作第一。一旦你寫了一個文件(你可以通過在文本編輯器中查看它來驗證是否正確),那麼你可以嘗試處理另一個腳本中的閱讀內容。 – abarnert 2014-10-30 20:22:47

回答

1

The tutorial涵蓋了這個很好,正如MattDMo指出的那樣。但我會在這裏總結相關部分。

關鍵的想法是打開一個文件,然後以某種格式寫每個totaal,然後確保文件在最後被關閉。

什麼格式?那麼,這取決於你的數據。有時候你有固定形狀的記錄,你可以將它們存儲爲CSV行。有時你有任意的Python對象,你可以將它們存儲爲pickles。但在這種情況下,您可以根本不使用最簡單的格式:一行文本。只要你的數據是可以明確地轉換成文本和返回的單個值,並且沒有任何換行符或其他特殊字符,就可以工作。所以:

with open('thefile.txt', 'w') as f: 
    while i < aantal_devices: 
     print(voorwerp[i].naam_device,voorwerp[i].stroomverbruik) 
     #dit totaal moet nog worden geschreven naar een bestand zodat je na 256 invoeren een totaal kan bepalen. 
     totaal = totaal + voorwerp[i].stroomverbruik 
     f.write('{}\n'.format(totaal)) 
     i = i + 1 

就是這樣。 open打開文件,如有必要創建它。 with確保它在塊的結尾處關閉。 write寫入一行,其中包含totaal中的任何內容,格式爲字符串,後跟換行符。

要在以後讀回就更簡單了:

with open('thefile.txt') as f: 
    for line in f: 
     totaal = int(line) 
     # now do stuff with totaal 
+0

哇謝謝,這是一個很大的幫助,這部分是幹什麼的? f.write('{} \ n'.format(totaal)) 我對編程並不陌生,但我一直在編寫其他語言的另一部分,所以我仍然需要學習很多:) ...問題是我需要知道如何在明天之前在我的編碼中編寫和閱讀並執行它... 真的非常感謝您的幫助! <3 – 2014-10-30 21:02:11

+0

@RicoDutchboyYTBOtto:答案是:「write'寫了一個由'totaal'中的任意內容組成的行,格式爲一個字符串,後跟一個換行符。你不明白的是哪部分? – abarnert 2014-10-30 21:14:10

0

使用序列化將數據存儲在文件中,然後將它們反序列化回原始狀態進行計算。

通過串行數據可以恢復數據到原來的狀態(值和類型,即1234int,而不是作爲字符串)

關你去文檔:):https://docs.python.org/2/library/pickle.html 附:爲了讓人們能夠幫助你,他的代碼需要可讀,這樣你可以在未來得到更好的答案。

+0

感謝您的評論,下次我會收到英文代碼! :) – 2014-10-30 20:37:44

0

您可以將它們寫入一個文件,像這樣:

with open(os.path.join(output_dir, filename), 'w') as output_file: 
    output_file.write("%s" % totaal) 

再總結他們是這樣的:

sum = 0 
for input_file in os.listdir(output_dir): 
    if os.path.isfile(input_file): 
     with open(os.path.join(output_dir, input_file), 'r') as infile: 
      sum += int(infile.read()) 
print sum/14 

不過,我會考慮是否真的需要寫每totaal到單獨的文件。可能有更好的方法來解決你的問題,但我認爲這應該做你所要求的。

P.S.我會嘗試閱讀你的代碼,並做出更有教養的嘗試來幫助你,但我不知道荷蘭語!

+0

oops hehe yeah通常我會寫英文評論,但我的一個同學想讓它成爲荷蘭人,這樣他就能明白我想做什麼) – 2014-10-30 20:35:03