Python 将浮点列表写入 csv 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21433484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:50:24  来源:igfitidea点击:

write float list to csv file

pythoncsv

提问by ChangeMyName

I have a list of float numbers, its format is:

我有一个浮点数列表,它的格式是:

float_list = [1.13, 0.25, 3.28, ....]

I used following code to write it to a .csv file:

我使用以下代码将其写入 .csv 文件:

float_str = ""

for result in result_list:
    float_str = float_str + str(result.get_value()) + ","

with open(output_path, "wb") as file:
    writer = csv.writer(file)
    writer.writerow(float_str)

file.close()

where result.get_value()is:

在哪里result.get_value()

def get_value():
    return float(value)

I expect to see that in each cell, there is a value. However, what I've got looks like:

我希望看到在每个单元格中都有一个值。但是,我所拥有的看起来像:

1   .   1   3   ,    0   .   2    5   ,  3   .   2   8   ,

It seems that each of the digits occupies a cell.

似乎每个数字都占据一个单元格。

So, may I know how can I solve this issue and get what I expected?

那么,我可以知道如何解决这个问题并达到我的预期吗?

采纳答案by Martijn Pieters

The writer.writerow()takes a sequence(a list or tuple), but you are passing in a string instead. By passing in a string, writer.writerow()still treats it as as sequence, and each individual character becomes a column value:

writer.writerow()需要一个序列(列表或元组),但你传递一个字符串代替。通过传入一个字符串,writer.writerow()仍然将其视为序列,并且每个单独的字符都成为一个列值:

1,.,1,3,,,0,.,2,5,,,3,.,2,8

Moreover, the method converts column to strings for you, don't do this yourself.

此外,该方法会为您将列转换为字符串,请不要自己执行此操作。

Instead build a list of your float values and pass that in:

而是构建一个浮点值列表并将其传入:

row = []
for result in result_list:
    row.append(result.get_value())

with open(output_path, "wb") as file:
    writer = csv.writer(file)
    writer.writerow(row)

or even:

甚至:

with open(output_path, "wb") as file:
    writer = csv.writer(file)
    writer.writerow([r.get_value() for r in result_list])

The delimiter defaults to ,.

分隔符默认为,

回答by Kobi K

Use delimiterand just write the list you don't need to convert it to string:

使用delimiter只需编写您不需要将其转换为字符串的列表:

import csv

float_list = [1.13, 0.25, 3.28]

with open(output_path, "wb") as file:
    writer = csv.writer(file, delimiter=',')
    writer.writerow(float_list)

Output:

输出:

1.13,0.25,3.28

1.13,0.25,3.28

Also there is no needto close the file since you used with

由于您使用过,因此也无需关闭文件with

Edit:

编辑:

@Martijn Pieters: delimiter ,is default.

@Martijn Pieters:分隔符,是默认值。

From Docs:

文档

A one-character string used to separate fields. It defaults to ','.

用于分隔字段的单字符字符串。它默认为“,”。

This line writer = csv.writer(file, delimiter=',')can be writer = csv.writer(file)

这条线writer = csv.writer(file, delimiter=',')可以writer = csv.writer(file)

回答by Yong he

I just want to write a number, like 1234, into csv file (delimiter=' '), it ends up like 1 2 3 4. I got the following answer from another site but would like to share it here:

我只想写一个数字,比如 1234,到 csv 文件 ( delimiter=' ') 中,结果是 1 2 3 4。我从另一个网站得到了以下答案,但想在这里分享:

number = 1.13
writer.writerow([number])

回答by Omid Moradipour

The below code writes a list of float numbers into a csv file.

以下代码将浮点数列表写入 csv 文件。

import csv

float_str=['1.500','2.48933','7.36945']

with open('output_path','w',newline='') as file:

    write=csv.writer(file, delimiter='\n')
    for num in float_str:
        write.writerow(num)