简体   繁体   English

如何将浮点数写入 CSV 文件,Python 中不带逗号?

[英]How to write float numbers to CSV file, without comma in Python?

Imagine that I have a list like this:想象一下,我有一个这样的列表:

list1 = [12.34, 17.68, 19.22]

Now I want to write each numbers in one row of a CSV file.现在我想将每个数字写在 CSV 文件的一行中。 So after that I changed the type of numbers to string, I do this:所以在那之后我将数字类型更改为字符串,我这样做:

with open(output_file_name, 'w') as ofile:
        outfile = csv.writer(ofile)
        outfile.writerows(list1)

and the result will be something like this:结果将是这样的:

1,2,.,3,4
1,7,.,6,8
1,9,.,2,2

How can I delete these commas between the numbers?如何删除数字之间的这些逗号?

Note:笔记:

  1. I want to show each number in a row我想连续显示每个数字
  2. I don't want anything for delimiter.我不想要任何分隔符。
  3. I use Python 3.9.7我用的是 Python 3.9.7
  4. I know that.csv file might not be suitable for this case, but this is a project that somebody wants from me, as an exam !我知道 .csv 文件可能不适合这种情况,但这是有人要我做的一个项目,作为考试!

You are passing list1 as the list of rows, so the csv module does its best to figure out what you mean.您将list1作为行列表传递,因此csv模块会尽力弄清楚您的意思。

Probably what you actually mean is可能你真正的意思是

    outfile.writerow(list1)

(notice the singular writerow ) which writes all the values as a single row. (注意单数writerow )将所有值写入一行。

Or, if you want one number per row, try或者,如果您想要每行一个数字,请尝试

    outfile.writerows([x] for x in list1)

In your attempt, list1 is converted to a list of strings, and then each string is split into a sequence to produce a row.在您的尝试中, list1被转换为字符串列表,然后每个字符串被拆分成一个序列以生成一行。

To reiterate, the argument to csv.writer.writerow should be a list of cell values, and the argument to writerows should be a list of such rows.重申一下, csv.writer.writerow的参数应该是一个单元格值列表,而writerows的参数应该是这些行的列表。

writerows will treat each element of the array as an row and attempt to iterate over the entries in that row, that is why for a string it splits it into individual characters. writerows 会将数组的每个元素视为一行并尝试迭代该行中的条目,这就是为什么对于字符串它将其拆分为单个字符的原因。 if you want to write each number on a separate line then use:如果您想将每个数字写在单独的行上,请使用:

with open(output_file_name, 'w') as ofile:
        outfile = csv.writer(ofile)
        outfile.writerows(([str(i)] for i in list1))

Use pathlib to easily write things使用pathlib轻松写东西

from pathlib import Path

# create file and write text
out_file = Path('example.csv')

# data
list_1 = [12.34 , 17.68 , 19.22]

out_file.write_text('column1, ')
with out_file.open(mode='a') as f:
    # write to file
    {f.write(f'\n{item}, ') for item in list_1}
    
    
# read contents
contents = out_file.read_text()
print(contents)
import csv

list1 = [12.34, 17.68, 19.22]

with open('output_file_name', 'w') as ofile:
    outfile = csv.writer(ofile, delimiter=" ")
    outfile.writerow(list1)

This should works for You I have checked it out in editor:-D Best regards;-)这应该对你有用我已经在编辑器中检查过了:-D 最好的问候;-)

One quick way to do this, would be to make use of the .join() method.一种快速的方法是使用.join()方法。 This method can take items from an iterator (such as your list) and join them together separated by whatever character(s) you'd like.此方法可以从迭代器(例如您的列表)中获取项目并将它们连接在一起,由您想要的任何字符分隔。 You can do it like this to have your items separated by only a space:您可以这样做,让您的项目仅由一个空格分隔:

with open(output_file_name, 'w') as ofile:
    outfile = csv.writer(ofile)
    outfile.writerows(' '.join(list1))

If you don't want a space between the items, but instead want them all stuck together do this instead:如果您不希望项目之间有空格,而是希望它们全部粘在一起,请改为执行以下操作:

outfile.writerows(''.join(list1))

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

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