[英]How to display a NumPy array of strings without quotes and with comma as a delimiter?
I am trying to generate an array out of a file with numpy.genfromtxt
.我正在尝试使用
numpy.genfromtxt
从文件中生成一个数组。
File is like:文件是这样的:
16.37.235.200|59009|514|16.37.235.153|
17.37.235.200|59009|514|18.37.235.153|
And I get an array like:我得到一个数组,如:
['16.37.235.200' '17.37.235.200']
But I want the array to be like that:但我希望数组是这样的:
[16.37.235.200,17.37.235.200]
Here is your original array:这是您的原始数组:
x = np.array(['16.37.235.200', '17.37.235.200'])
which is displayed like this when printed:打印时显示如下:
print(x)
>>> ['16.37.235.200' '17.37.235.200']
In order to display it with comma as a delimiter and without quotes around strings we can use np.array2string
:为了使用逗号作为分隔符并且在字符串周围没有引号,我们可以使用
np.array2string
:
print(np.array2string(x, separator=',', formatter={'str_kind': lambda x: x}))
>>> [16.37.235.200,17.37.235.200]
I don't like that lambda x: x
formatter but couldn't come up with something better to remove the quotes.我不喜欢那个
lambda x: x
格式化程序,但无法想出更好的方法来删除引号。
You can find more here: How to pretty-printing a numpy.array without scientific notation and with given precision?您可以在此处找到更多信息: 如何在没有科学记数法和给定精度的情况下漂亮地打印 numpy.array?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.