我正在尝试使用 numpy.genfromtxt
从文件中生成一个数组。
文件如下:
16.37.235.200|59009|514|16.37.235.153|
17.37.235.200|59009|514|18.37.235.153|
然后我得到一个像这样的数组:
['16.37.235.200' '17.37.235.200']
但我希望数组是这样的:
[16.37.235.200,17.37.235.200]
最佳答案
这是你的原始数组:
x = np.array(['16.37.235.200', '17.37.235.200'])
打印出来是这样的:
print(x)
>>> ['16.37.235.200' '17.37.235.200']
为了以逗号作为分隔符显示它并且不在字符串周围使用引号,我们可以使用 np.array2string
:
print(np.array2string(x, separator=',', formatter={'str_kind': lambda x: x}))
>>> [16.37.235.200,17.37.235.200]
我不喜欢那个 lambda x: x
格式化程序,但想不出更好的方法来删除引号。
您可以在这里找到更多信息:How to pretty-printing a numpy.array without scientific notation and with given precision?
关于python - 如何显示不带引号且以逗号作为分隔符的 NumPy 字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52667328/