将数字连接到一个文件中

joins numbers together into a file

我有一个数字列表,我想将它们加入或一起写入一个文本文件,并将数字的总和放入文件中。

[0.297, 0.344, 0.342, 0.285, 0.333, 0.337, 0.309, 0.334, 0.337, 0.289, 0.342, 0.337, 0.292, 0.337, 0.347, 0.31, 0.335, 0.34, 0.279, 0.331, 0.336, 0.298, 0.338, 0.347, 5.357, 0.342, 0.314, 0.289, 0.358, 0.341, 0.321, 0.336, 0.347, 0.3, 0.336, 0.333, 0.279, 0.334, 0.339, 0.303, 0.335, 0.337, 0.301, 0.329, 0.337, 0.289, 0.335, 0.341, 0.305, 0.332, 0.347, 0.307, 0.332, 0.344, 0.291, 0.334, 0.333, 0.347, 0.345, 0.342, 0.297, 0.338, 0.35, 0.329, 0.336, 0.345, 0.309, 0.344, 0.344, 0.288, 0.332, 0.341, 0.311, 0.343, 0.344, 0.319, 0.332, 0.335, 0.297, 0.337, 0.344, 0.297, 0.334, 0.341, 0.306, 0.332, 0.335, 0.299, 0.332, 0.344, 0.286, 0.332, 0.344, 5.362, 0.348, 0.318, 0.296, 0.345, 0.338, 0.305, 0.343, 0.343, 0.297, 0.332, 0.331, 0.296, 0.334, 0.336, 0.305, 0.341, 0.346, 0.286, 0.337, 0.334, 0.306, 0.342, 0.342, 0.305, 0.332, 0.335, 0.285, 0.331, 0.335, 0.285, 0.334, 0.343, 0.308, 0.333, 0.341, 0.286, 0.338, 0.336, 0.306, 0.342, 0.335, 0.305, 0.342, 0.332, 0.296, 0.332, 0.342, 0.296, 0.342, 0.336, 0.307, 0.343, 0.347, 0.313, 0.328, 0.344, 0.313, 0.338, 0.334, 0.299, 0.337, 0.35, 0.292, 0.348, 0.341, 0.289, 0.335, 0.339, 5.366, 0.358, 0.343, 0.3, 0.338, 0.341, 0.306, 0.343, 0.345, 0.296, 0.332, 0.352, 0.296, 0.333, 0.346, 0.306, 0.342]

我希望它们在一行中并将总和放在行的顶部,例如

sum = 2223.2
numbers are 
0.322
0.223
0.312

我尝试使用 float 加入,但出现错误

blut = '\n'.join(float(diffs))
open('ext.txt', 'w').write(blut)

您可以先将diff中的所有数字转换为字符串,然后用\n连接它们:

output = list(map(str, diffs))
blut = '\n'.join(output)

额外的 list() 用于 Python 3.X 并且在 Python 2.7 中是多余的。这假设您的初始列表称为 diffs.

有更多方法可以创建目标输出文件。例如,您可以使用 join 一次写入总和、文本和数字,

with open('ext.txt', 'w') as fout:
    fout.write(''.join(['sum = ', str(sum(diffs)), '\nnumbers are\n', blut]))

这里的总和也是直接在输入列表中计算到join并转换为字符串。 blut 已经是一个字符串列表。 join 使用空字符串作为分隔符,因为所有必要的空格和换行符都包含在输入列表中。

另一种方法是拆分书写并先添加总和,然后是文本,然后是数字。在这种情况下,您需要更改 w 以在第一次写入后追加,即 a

l = [0.297, 0.344, 0.342, 0.285, 0.333, 0.337, 0.309, 0.334, 0.337, 0.289, 0.342, 0.337, 0.292, 0.337, 0.347, 0.31, 0.335, 0.34, 0.279, 0.331, 0.336, 0.298, 0.338, 0.347, 5.357, 0.342, 0.314, 0.289, 0.358, 0.341, 0.321, 0.336, 0.347, 0.3, 0.336, 0.333, 0.279, 0.334, 0.339, 0.303, 0.335, 0.337, 0.301, 0.329, 0.337, 0.289, 0.335, 0.341, 0.305, 0.332, 0.347, 0.307, 0.332, 0.344, 0.291, 0.334, 0.333, 0.347, 0.345, 0.342, 0.297, 0.338, 0.35, 0.329, 0.336, 0.345, 0.309, 0.344, 0.344, 0.288, 0.332, 0.341, 0.311, 0.343, 0.344, 0.319, 0.332, 0.335, 0.297, 0.337, 0.344, 0.297, 0.334, 0.341, 0.306, 0.332, 0.335, 0.299, 0.332, 0.344, 0.286, 0.332, 0.344, 5.362, 0.348, 0.318, 0.296, 0.345, 0.338, 0.305, 0.343, 0.343, 0.297, 0.332, 0.331, 0.296, 0.334, 0.336, 0.305, 0.341, 0.346, 0.286, 0.337, 0.334, 0.306, 0.342, 0.342, 0.305, 0.332, 0.335, 0.285, 0.331, 0.335, 0.285, 0.334, 0.343, 0.308, 0.333, 0.341, 0.286, 0.338, 0.336, 0.306, 0.342, 0.335, 0.305, 0.342, 0.332, 0.296, 0.332, 0.342, 0.296, 0.342, 0.336, 0.307, 0.343, 0.347, 0.313, 0.328, 0.344, 0.313, 0.338, 0.334, 0.299, 0.337, 0.35, 0.292, 0.348, 0.341, 0.289, 0.335, 0.339, 5.366, 0.358, 0.343, 0.3, 0.338, 0.341, 0.306, 0.343, 0.345, 0.296, 0.332, 0.352, 0.296, 0.333, 0.346, 0.306, 0.342]

with open("file.txt", "w+") as f:
    s = sum(l)
    f.write("sum = {}\n".format(s))
    f.write("\n".join(map(str, l)))