简体   繁体   中英

Mixed types in np array

I try to add data to a np array with mixed data, integers and strings. Fromnumpy.org i got:

To use actual strings in Python 3 use U or numpy.str_.

However, if i use U or numpy:str_ if get

invalid literal for int() with base 10: 'fileA'

So I am pretty sure I misinterpreted somethine about the string or dtype. How do i get columns for strings in a mixed array in numpy? Thanks!

import sys
import numpy as np

document_dtype = np.dtype({'names': ['global_line','filename', 'file_line','type','text'], 'formats': ['i','U','i','U','U']})
document = np.empty(shape=(0,5), dtype=document_dtype)
document = np.vstack([document, np.array([2,'fileA',1,'Header','Test'],dtype=document_dtype)])

Just use 'O' for Python object.

In Python all data types inherit from object . So both strings and integers are objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM