简体   繁体   English

使用 python 将列表列表写入文件,但每个元组必须在新行中

[英]write list of lists to file but each tuple has to be on a new line, using python

I have an ugly list of lists generated in a program, which looks like this:我在程序中生成了一个丑陋的列表列表,如下所示:

 a= [[93.400000000000006, "high"], [98.600000000000009, 99.0, "high"], [121.30000000000001, 124.1000000000000]]

I am saving it to a text file as follows:我将其保存到文本文件中,如下所示:

with open('sample.txt','a') as outfile:
    json.dump(a,outfile)
    outfile.write("\n\n")

When I open the text file, the values saved are an eyesore.当我打开文本文件时,保存的值很碍眼。 How do I save each list to a new line?如何将每个列表保存到新行?

For example if I wanted to print each list to a new line, I could simply do:例如,如果我想将每个列表打印到一个新行,我可以简单地这样做:

for i in range(len(a)):
    print a[i]

thank you谢谢你

EDIT: OUTPUT HAS TO LOOK LIKE:编辑:OUTPUT 必须看起来像:

[93.400000000000006, "high"] [93.400000000000006,“高”]
[98.600000000000009, 99.0, "high"] [98.600000000000009, 99.0, “高”]

ie each on one line.即每个在一行上。

I really don't any reason here to use json.dumps . 我真的没有任何理由在这里使用json.dumps You can just use a normal for loop: 您可以只使用普通的for循环:

a = [
        [93.400000000000006, "high"], 
        [98.600000000000009, 99.0, "high"], 
        [111.60000000000001, 112.5, "high"]
]

with open('sample.txt', 'a') as outfile:
    for sublist in a:
        outfile.write('{}\n'.format(sublist))

The above code produces the output: 上面的代码产生输出:

[93.400000000000006, 'high']
[98.600000000000009, 99.0, 'high']
[111.60000000000001, 112.5, 'high']

You can try: 你可以试试:

with open('sample.txt','a') as outfile:
    for item in a:
        json.dump(item,outfile)
        outfile.write("\n\n")

this should work for you: 这应该为您工作:

file=open('sample.txt','w')
for i in a:
    file.write(str(a))
    file.write("\n\n")

If you want the output to be in JSON format, but want the elements of the outer list to appear on separate lines in the file, supply the indent keyword argument:如果您希望 output 采用 JSON 格式,但希望外部列表的元素出现在文件中的单独行中,请提供indent关键字参数:

with open('sample.txt','w') as outfile:
    json.dump(a, outfile, indent=4)

Given a number or a whitespace string, indent specifies the indentation to use at each level, showing the structure of the data.给定一个数字或一个空白字符串, indent指定在每个级别使用的缩进,显示数据的结构。 By default, the output is not pretty-printed at all, to save space.默认情况下,为了节省空间,output 根本没有漂亮地打印出来。 See the documentation for details.有关详细信息,请参阅文档 The same argument works with json.dumps to create a string.相同的参数与json.dumps一起使用以创建一个字符串。

Note that the JSON data format is not designed to be "embedded" in other files;请注意,JSON 数据格式并未设计为“嵌入”在其他文件中; you should not use the 'a' file mode for writing JSON, and should not plan on the file containing anything else besides your one JSON value.你不应该使用'a'文件模式来写入 JSON,并且不应该计划包含除你的 JSON 值之外的任何其他内容的文件。

Another option is the pprint standard library module , if you aren't especially concerned with the data format.如果您不是特别关心数据格式,另一个选择是pprint标准库模块 pprint.pprint will automatically decide how to split up lines based on their length: pprint.pprint将自动决定如何根据长度拆分行:

>>> import pprint
>>> pprint.pprint([[1],[2],[3]])
[[1], [2], [3]]
>>> pprint.pprint([[1],[2],[3]], width=1) # force line-wrapping
[[1],
 [2],
 [3]]

It can print to a file by specifying the stream argument.它可以通过指定stream参数打印到文件。

Finally, it is worth noting the option of iterating over the list and using json.dump for each line.最后,值得注意的是遍历列表并为每一行使用json.dump的选项。 This does not produce valid JSON , but instead a related data format called JSONL.不会产生有效的 JSON ,而是产生一种称为 JSONL 的相关数据格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM