将坐标打印到 csv 文件
Printing Coordinates to a csv File
我想使用方括号和逗号分隔符格式 (x,y) 将坐标提取到 .csv 文件。
我有一个写成列表 (network1) 的 4x4 矩阵,需要识别出现 1 的坐标,然后将这些坐标导出到 .csv 文件。
下面的代码是由另一位用户建议的,它适用于不同的数据集,但我需要进一步调整它以适应这种格式。
我希望下面的现有代码只需要一个小的修改。
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)
这会产生如下输出(为了更清楚起见,我包含了文本文件中的结果):
对于这个特定的输出,我需要以下格式:
非常感谢您协助完成第二张图片中的以下内容:
- 打印 set A := 到 .csv 文件,不带引号 (" ")。
- 用括号打印坐标,仅用逗号分隔。
非常感谢您的帮助!
我看到你的问题了。现在解决这个问题..
df.to_csv(quotechar='"')
默认情况下 quotechar
是字符串。想想看。
所以试试这个...
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
输出:
set A :=
(1, 2)
(2, 3)
(3, 3)
(3, 4)
(4, 3)
(4, 4)
;
在这里使用 Pandas 有点矫枉过正。
这是该程序的简化版本,可以正确输出 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))
输出文件的内容:
x,y
0,1
1,2
2,2
2,3
3,2
3,3
如果要输出元组,请将上面的上下文管理器替换为以下内容:
with open("resources/text.txt", "w") as file_out:
for curr_coords in zip(*coords):
file_out.write(str(curr_coords) + '\n')
输出文件的内容:
(0, 1)
(1, 2)
(2, 2)
(2, 3)
(3, 2)
(3, 3)
我想使用方括号和逗号分隔符格式 (x,y) 将坐标提取到 .csv 文件。
我有一个写成列表 (network1) 的 4x4 矩阵,需要识别出现 1 的坐标,然后将这些坐标导出到 .csv 文件。
下面的代码是由另一位用户建议的,它适用于不同的数据集,但我需要进一步调整它以适应这种格式。
我希望下面的现有代码只需要一个小的修改。
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)
这会产生如下输出(为了更清楚起见,我包含了文本文件中的结果):
对于这个特定的输出,我需要以下格式:
非常感谢您协助完成第二张图片中的以下内容:
- 打印 set A := 到 .csv 文件,不带引号 (" ")。
- 用括号打印坐标,仅用逗号分隔。
非常感谢您的帮助!
我看到你的问题了。现在解决这个问题..
df.to_csv(quotechar='"')
默认情况下 quotechar
是字符串。想想看。
所以试试这个...
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
输出:
set A :=
(1, 2)
(2, 3)
(3, 3)
(3, 4)
(4, 3)
(4, 4)
;
在这里使用 Pandas 有点矫枉过正。
这是该程序的简化版本,可以正确输出 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))
输出文件的内容:
x,y
0,1
1,2
2,2
2,3
3,2
3,3
如果要输出元组,请将上面的上下文管理器替换为以下内容:
with open("resources/text.txt", "w") as file_out:
for curr_coords in zip(*coords):
file_out.write(str(curr_coords) + '\n')
输出文件的内容:
(0, 1)
(1, 2)
(2, 2)
(2, 3)
(3, 2)
(3, 3)