在python中将numpy、list或float转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24914735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert numpy, list or float to string in python
提问by jw21
I'm writing a python function to append data to text file, as shown in the following,
我正在编写一个python函数来将数据附加到文本文件中,如下所示,
The problem is the variable, var
, could be a 1D numpy array, a 1D list, or just a float number, I know how to convert numpy.array
/list
/float
to string separately (meaning given the type), but is there a method to convert var
to string without knowing its type?
问题是可变的,var
,可能是一维numpy的阵列,一维列表,或者只是一个浮点数,我知道如何转换numpy.array
/ list
/float
串单独(给定类型的意思),但有一种方法来转换var
为字符串不知道它的类型?
def append_txt(filename, var):
my_str = _____ # convert var to string
with open(filename,'a') as f:
f.write(my_str + '\n')
Edit 1: Thanks for the comments, sorry maybe my question was not clear enough.
str(var)
on numpy would give something like []
. For example, var = np.ones((1,3)), str(var)
will give [[1. 1. 1.]]
, and []
is unwanted,
编辑 1:感谢您的评论,抱歉,也许我的问题不够清楚。
str(var)
在 numpy 上会给出类似[]
. 例如,var = np.ones((1,3)), str(var)
会给[[1. 1. 1.]]
,并且[]
是不需要的,
Edit 2: Since I want to write clean numbers (meaning no [
or ]
), it seems type checking is inevitable.
编辑 2:由于我想写干净的数字(意思是 no[
或]
),似乎类型检查是不可避免的。
回答by rajeshv90
If you don't know what is the type of var you can check type using
如果您不知道 var 的类型是什么,则可以使用检查类型
from collections import Iterable
if isinstance(var,Iteratable):
mystring=''.join(map(str,var))
else:
mystring=str(var)
回答by DavidK
回答by Sudip Kafle
str
is able to convert any type into string. It can be numpy.array / list / float
str
能够将任何类型转换为字符串。它可以是 numpy.array/list/float
# using numpy array
new_array = numpy.array([1,2,3])
str(new_array)
>> '[1 2 3]'
# using list
new_list = [1, 2, 3]
str(new_list)
>> '[1, 2, 3]'
# using float
new_float = 1.1
str(new_float)
>> '1.1'
回答by Banana
Type checking is not the only option to do what you want, but definitely one of the easiest:
类型检查不是做你想做的事情的唯一选择,但绝对是最简单的选择之一:
import numpy as np
def to_str(var):
if type(var) is list:
return str(var)[1:-1] # list
if type(var) is np.ndarray:
try:
return str(list(var[0]))[1:-1] # numpy 1D array
except TypeError:
return str(list(var))[1:-1] # numpy sequence
return str(var) # everything else
EDIT:Another easy way, which does not use type checking(thanks to jtaylor for giving me that idea), is to convert everything into the same type (np.array
) and then convert it to a string:
编辑:另一种不使用类型检查的简单方法(感谢 jtaylor 给我这个想法)是将所有内容转换为相同类型 ( np.array
),然后将其转换为字符串:
import numpy as np
def to_str(var):
return str(list(np.reshape(np.asarray(var), (1, np.size(var)))[0]))[1:-1]
Example use(both methods give same results):
示例使用(两种方法给出相同的结果):
>>> to_str(1.) #float
'1.0'
>>> to_str([1., 1., 1.]) #list
'1.0, 1.0, 1.0'
>>> to_str(np.ones((1,3))) #np.array
'1.0, 1.0, 1.0'