简体   繁体   English

如何显示不带引号且以逗号作为分隔符的 NumPy 字符串数组?

[英]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.

相关问题 numpy savetxt 没有添加逗号分隔符 - numpy savetxt is not adding comma delimiter 带有逗号分隔符和引号的CSV文件,但并非每行 - CSV file with comma delimiter and quotes, but not on every line numpy.loadtxt:如何忽略引号内出现的逗号分隔符? - numpy.loadtxt: how to ignore comma delimiters that appear inside quotes? 如何将字符串分配给数组中的不同元素,对数组进行排序,然后根据 Python/Numpy 中的排序显示字符串? - How can I assign strings to different elements in an array, sort the array, then display the strings based on the sort in Python/Numpy? Python 用逗号和引号分解字符串 - Python explode strings with comma and quotes 将逗号分隔的字符串分配给元组数组-python,numpy - Assigning comma separated strings to an array of tuples - python, numpy 如何在csv中使用带有逗号的字符串给列加双引号 - How to give double quotes to column with strings that have comma's in csv pprint():如何使用双引号来显示字符串? - pprint(): how to use double quotes to display strings? 在逗号分隔符的csv中读取带有逗号和引号的字段-pyspark - To read a field with comma and quotes in csv where comma is delimiter - pyspark 用逗号作为分隔符分隔字符串和其他值....! - Separating Strings and other values with comma as a delimiter....!
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM