[英]Printing Coordinates to a csv File
I would like to extract coordinates to a .csv file with the brackets and comma delimiter format (x,y).我想使用括号和逗号分隔符格式 (x,y) 将坐标提取到 .csv 文件中。
I have a 4x4 matrix written as a list (network1) and need to identify the coordinates where a 1 occurs to then export these coordinates to a .csv file.我有一个 4x4 矩阵写为列表 (network1),需要确定出现 1 的坐标,然后将这些坐标导出到 .csv 文件。
The code below was suggested by another user which works great for a different set of data, although I need to adjust this a bit further to suit this format.下面的代码是由另一个用户建议的,它适用于不同的数据集,尽管我需要进一步调整它以适应这种格式。
I expect there is only a small modification needed in the existing code which is below.我希望在下面的现有代码中只需要进行一些小的修改。
import numpy as np
import pandas as pd
network1 = [0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,1]
network1_matrix = np.array(network1).reshape(4, 4)
coordinates = np.transpose(np.where(network1_matrix == 1))
result_df = pd.DataFrame({'1': coordinates[:, 0] + 1, '2': coordinates[:, 1] + 1})
result_df = result_df.append({'1': ';', '2': ''}, ignore_index=True)
result_df.columns = ['set A :=', '']
result_df.to_csv('result.csv', sep=' ', index=False)
This produces an output as follows (I have included results from a text file for greater clarity):这会产生如下输出(为了更清晰,我已经包含了文本文件的结果):
For this specific output, I need it in the following format:对于此特定输出,我需要采用以下格式:
I would greatly appreciate the assistance to complete the following as per the second image:我将非常感谢按照第二张图片完成以下任务的帮助:
Thanks a lot for the help!非常感谢您的帮助!
I have seen your problem.我已经看到你的问题了。 Now fix this issue..
现在解决这个问题..
df.to_csv(quotechar='"')
bydefault quotechar
is string.默认
quotechar
是字符串。 Think about it.想想看。
So try this like...所以试试这个......
import numpy as np
import pandas as pd
network1 = [0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,1]
network1_matrix = np.array(network1).reshape(4, 4)
"""
array([[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 1, 1]])
"""
coordinates = np.transpose(np.where(network1_matrix == 1))
"""
array([[0, 1],
[1, 2],
[2, 2],
[2, 3],
[3, 2],
[3, 3]])
"""
result_df = pd.DataFrame({'1': coordinates[:, 0] + 1, '2': coordinates[:, 1] + 1})
result_df.columns = ['set', '']
result_df['set A :='] = result_df[['set', '']].apply(tuple, axis=1)
result_df = result_df.append({'set A :=': ';'}, ignore_index=True)
#result_df
result_df = result_df['set A :=']
result_df.to_csv('result.csv', sep=' ', float_format = True, index = False, quotechar= '\r')
!head result.csv
Output:输出:
set A :=
(1, 2)
(2, 3)
(3, 3)
(3, 4)
(4, 3)
(4, 4)
;
Using Pandas is overkill here.在这里使用 Pandas 太过分了。
Here is a simplified version of the program, which correctly outputs CSV:这是程序的简化版本,可以正确输出 CSV:
import csv
import numpy as np
arr = [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1]
arr = np.array(arr).reshape(4, 4)
print(arr)
coords = np.where(arr == 1)
print(coords)
with open("resources/test.csv", "w") as file_out:
writer = csv.writer(file_out)
writer.writerow(('x', 'y'))
writer.writerows(zip(*coords))
Contents of the output file:输出文件的内容:
x,y
0,1
1,2
2,2
2,3
3,2
3,3
If you want to output tuples, replace the context manager above with the following:如果要输出元组,请将上面的上下文管理器替换为以下内容:
with open("resources/text.txt", "w") as file_out:
for curr_coords in zip(*coords):
file_out.write(str(curr_coords) + '\n')
Contents of the output file:输出文件的内容:
(0, 1)
(1, 2)
(2, 2)
(2, 3)
(3, 2)
(3, 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.