QWE*_*XCV 3 python arrays numpy genfromtxt
我正在尝试从文件中生成一个数组numpy.genfromtxt。
文件是这样的:
16.37.235.200|59009|514|16.37.235.153|
17.37.235.200|59009|514|18.37.235.153|
Run Code Online (Sandbox Code Playgroud)
我得到一个像这样的数组:
['16.37.235.200' '17.37.235.200']
Run Code Online (Sandbox Code Playgroud)
但我希望数组是这样的:
[16.37.235.200,17.37.235.200]
Run Code Online (Sandbox Code Playgroud)
这是你的原始数组:
x = np.array(['16.37.235.200', '17.37.235.200'])
Run Code Online (Sandbox Code Playgroud)
打印时显示如下:
print(x)
>>> ['16.37.235.200' '17.37.235.200']
Run Code Online (Sandbox Code Playgroud)
为了使用逗号作为分隔符并且在字符串周围不带引号来显示它,我们可以使用np.array2string:
print(np.array2string(x, separator=',', formatter={'str_kind': lambda x: x}))
>>> [16.37.235.200,17.37.235.200]
Run Code Online (Sandbox Code Playgroud)
我不喜欢那个lambda x: x格式化程序,但无法想出更好的方法来删除引号。
您可以在这里找到更多信息:如何在没有科学记数法和给定精度的情况下漂亮地打印 numpy.array?