[英]How to write any number of elements of a list in one line in a CSV file?
I want to write a CSV file with a specific format, so I can train with the data.我想用特定格式编写一个 CSV 文件,以便我可以使用数据进行训练。 I need every information per input in one line.
我需要一行中的每个输入的所有信息。 The charList is a list of chars (with length 108) in ascii numbers.
charList 是 ASCII 数字中的字符列表(长度为 108)。
enter image description here在此处输入图片说明
Instead of put the list in the csv, I want to have every char seperately.而不是将列表放在 csv 中,我想单独拥有每个字符。 So one CSV line should look like:
因此,一行 CSV 应如下所示:
0.2344,0.234565,0.245534,45,27,34...34 0.2344,0.234565,0.245534,45,27,34...34
You didn't show example input data and what result you get at this moment so I will use my data.您没有显示示例输入数据以及此时您得到的结果,因此我将使用我的数据。
You have to flatten
it.你必须把它
flatten
。 You can use +
to join lists您可以使用
+
加入列表
If you have如果你有
data = [12, 34, ["A", "B"], 56]
where data[2]
is ["A", "B"]
then其中
data[2]
是["A", "B"]
然后
result = data[:2] + data[2] + data[3:]
gives给
[12, 34, "A", "B", 56]
If you have如果你有
data = [12, 34, "AB", 56]
then you can use list
to convert string into list然后您可以使用
list
将字符串转换为列表
result = data[:2] + list(data[2]) + data[3:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.