需要帮助用Python编写和阅读

时间:2014-10-30 20:15:45

标签: 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个输入并给出它们的总和并除以该数字如果有人可以帮助我在正确的轨道上写下这些值并稍后阅读它们,我可以尝试找出如何做最后一部分。

但我现在已经尝试了2天,仍然没有找到写作和阅读的好方法。

3 个答案:

答案 0 :(得分:1)

正如The tutorial指出的那样,

MattDMo非常清楚地涵盖了这一点。但我会在这里总结相关部分。

关键的想法是打开一个文件,然后以某种格式编写每个totaal,然后确保文件最后关闭。

什么格式?那么,这取决于你的数据。有时您有固定形状的记录,您可以将其存储为CSV行。有时您有任意Python对象,您可以将其存储为pickle。但在这种情况下,你可以使用最简单的格式:一行文字。只要您的数据是可以明确地转换为文本和返回的单个值,并且没有任何换行符或其他特殊字符,这就可以了。所以:

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

答案 1 :(得分:0)

使用序列化将数据存储在文件中,然后将它们反序列化回原始状态以进行计算。

通过序列化数据,您可以将数据恢复到原始状态(值和类型,即1234int而不是字符串)

关闭你去文档:):https://docs.python.org/2/library/pickle.html 附:对于能够帮助您的人来说,他的代码需要具有可读性,这样您将来可以获得更好的答案。

答案 2 :(得分: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。我会尝试阅读你的代码并做一个更有教养的尝试来帮助你,但我不懂荷兰语!