I have this list list of values:
我有这个列表的值列表:
Output of list_a:
list_a的输出:
list_a: [[[2.0, 4.0], [1.0, 2.0]], [[2.0, 4.0], [2.0, 3.0], [3.0, 1.0]], [[3.0, 6.0], [5.0, 5.0], [6.0, 4.0]], [[3.0, 6.0], [4.0, 7.0]], [[2.0, 4.0], [3.0, 6.0]]]
I want to export it to a csv file in a specific format so that every set of values occupies a cell in the following format:
我想以特定格式将其导出到csv文件,以便每组值都按以下格式占用一个单元格:
(2.0; 4.0) | (1.0; 2.0)
(2.0; 4.0) | (2.0; 3.0) | (3.0; 1.0)
...
The "|" represents the separation of cells on the same row (not to be included in the csv file) and the format of the values in the cell should be parentheses and semicolon as such (X1; X2)
“|”表示同一行上的单元格分隔(不包含在csv文件中),单元格中值的格式应为圆括号和分号(X1; X2)
I tried the following but I get brackets and colons:
我试过以下但我得到了括号和冒号:
with open('outputdata.csv', 'w') as outfile:
mywriter = csv.writer(outfile)
for d in result:
mywriter.writerow(d)
Any help is appreciated!
任何帮助表示赞赏!
3 个解决方案
#1
0
You can try to write the csv yourself, without any writer class:
您可以尝试自己编写csv,而不需要任何编写器类:
with open('outputdata.csv', 'w') as f:
for vector in list_a:
len_ = len(vector)
for i, point in enumerate(vector):
f.write("({}; {})".format(*point))
# avoid writing the last pipe
if i != len_ - 1:
f.write(" | ")
f.write("\n")
Contents:
(2.0; 4.0) | (1.0; 2.0)
(2.0; 4.0) | (2.0; 3.0) | (3.0; 1.0)
(3.0; 6.0) | (5.0; 5.0) | (6.0; 4.0)
(3.0; 6.0) | (4.0; 7.0)
(2.0; 4.0) | (3.0; 6.0)
#2
0
try this:
with open('outputdata.csv', 'w') as outfile:
mywriter = csv.writer(outfile)
for d in result:
row = ' | '.join(['(%f; %f)' % (t[0], t[1]) for t in d])
mywriter.writerow(d)
Each element of each row is an array of of two floats, right? So, we use a format string to convert [2.0, 4.0]
to "(2.0; 4.0)"
. We do this for every tuple in the row. Then, we just use python's join()
function to put the characters |
between them and link them all into one string, which we can write to the file.
每行的每个元素都是两个浮点数的数组,对吗?因此,我们使用格式字符串将[2.0,4.0]转换为“(2.0; 4.0)”。我们为行中的每个元组执行此操作。然后,我们只使用python的join()函数来放置字符|在它们之间并将它们全部链接到一个字符串中,我们可以将其写入文件。
#3
0
It may be easier to generate the output file without the writer:
在没有编写器的情况下生成输出文件可能更容易:
SEP = ',' # Use any other separator here, if you want
with open('outputdata.csv', 'w') as outfile:
for line in list_a:
outfile.write(SEP.join(['({}; {})'.format(*pair) for pair in line]))
outfile.write("\n")
#1
0
You can try to write the csv yourself, without any writer class:
您可以尝试自己编写csv,而不需要任何编写器类:
with open('outputdata.csv', 'w') as f:
for vector in list_a:
len_ = len(vector)
for i, point in enumerate(vector):
f.write("({}; {})".format(*point))
# avoid writing the last pipe
if i != len_ - 1:
f.write(" | ")
f.write("\n")
Contents:
(2.0; 4.0) | (1.0; 2.0)
(2.0; 4.0) | (2.0; 3.0) | (3.0; 1.0)
(3.0; 6.0) | (5.0; 5.0) | (6.0; 4.0)
(3.0; 6.0) | (4.0; 7.0)
(2.0; 4.0) | (3.0; 6.0)
#2
0
try this:
with open('outputdata.csv', 'w') as outfile:
mywriter = csv.writer(outfile)
for d in result:
row = ' | '.join(['(%f; %f)' % (t[0], t[1]) for t in d])
mywriter.writerow(d)
Each element of each row is an array of of two floats, right? So, we use a format string to convert [2.0, 4.0]
to "(2.0; 4.0)"
. We do this for every tuple in the row. Then, we just use python's join()
function to put the characters |
between them and link them all into one string, which we can write to the file.
每行的每个元素都是两个浮点数的数组,对吗?因此,我们使用格式字符串将[2.0,4.0]转换为“(2.0; 4.0)”。我们为行中的每个元组执行此操作。然后,我们只使用python的join()函数来放置字符|在它们之间并将它们全部链接到一个字符串中,我们可以将其写入文件。
#3
0
It may be easier to generate the output file without the writer:
在没有编写器的情况下生成输出文件可能更容易:
SEP = ',' # Use any other separator here, if you want
with open('outputdata.csv', 'w') as outfile:
for line in list_a:
outfile.write(SEP.join(['({}; {})'.format(*pair) for pair in line]))
outfile.write("\n")