python-3.x - 将多个数据写入 csv,np 数组被转换为字符串
问题描述
我正在尝试将 pixel_array 数据和 PatientID 从多个 Dicom 文件写入 CSV。
像素数组,它们是 np 数组,像这样保存;
[[0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n ...\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]\n [0 0 0 ... 0 0 0]]
如何取回原始的 np 数组?
如果你能给我一些关于将这些正确地写入 CSV 的建议,以便 np 数组保持原样?下面是循环
def tryer(a, b):
if b == 'pixel_array':
return(a.pixel_array)
else:
try:
return(getattr(a, b))
except AttributeError:
return('No')
tryer 定义如上;
a_list = ['PatientID', 'pixel_array']
with open('imgs_n_patied_train1.csv', 'w', newline = '') as f:
thewriter = writer(f)
thewriter.writerow(['PatientID', 'pixel_array\n'])
for i in imgs:
#iterating through each file and writing all the metadata to a csv row-wise
a = dicom.read_file(r'pathto\stage_2_train_images' + '\\' + i, force = True)
app = []
for j in a_list:
app.append(tryy.tryer(a, j))
thewriter.writerow(app)
解决方案
推荐阅读
- python - 什么时候安装 imgaug.py 我得到这个错误
- java - 如何使用 CsvReader 中的 getHeaders() 方法读取 csv 文件的标头值
- c# - 试图在dotnet端绑定来自oracle的多个值
- javascript - 通过升序/降序对两个数组内的对象进行排序
- mysql - 如何从 WordPress 的 UserMeta 表中删除特定的 meta_key 字段及其值?
- jmeter - 如何使用 jmeter 测试 webhook?
- c# - 使用 SQL 数据库的依赖注入刷新 ASP.NET Core 中的 Azure Active Directory 访问令牌
- iis - 如何配置 IIS 以将无效的 http 请求转发到 .net 核心
- c++ - 操作员|| std::variant 的重载
- c# - SQL 选择查询 - 日期时间中没有秒或毫秒