2014-04-22 7 views
15

से एचडीएफ 5 डेटासेट में तारों की एक सूची संग्रहीत करना मैं स्ट्रिंग की एक चर लंबाई लंबाई एचडीएफ 5 डेटासेट में स्टोर करने की कोशिश कर रहा हूं। जहां & lt प्रतीक
कैसे की तुलना में वास्तविक कम का मतलब है इस के लिए कोडपायथन

import h5py 
h5File=h5py.File('xxx.h5','w') 
strList=['asas','asas','asas'] 
h5File.create_dataset('xxx',(len(strList),1),'S10',strList) 
h5File.flush() 
h5File.Close() 

मैं हुए कहा कि ": dtype के लिए कोई रूपांतरण पथ dtype ('& lt U3') लेखन त्रुटि" एक त्रुटि हो रही है है क्या मैं इस समस्या को हल कर सकता हूं।

+0

स्टार्टर्स के लिए, आपके पास 'create_dataset' पर एक टाइपो है। क्या आप सटीक कोड का उपयोग कर रहे हैं, खासकर जहां 'strList' आ रहा है? – SlightlyCuban

+0

टाइपो के बारे में खेद है, मैं तो मैं एक हैडर सभी स्तंभों के नाम तो मैं किसी सूची में स्तंभ नाम निकाला जाता है और यह एक करने के लिए लिखने की कोशिश कर होता है बनाने के लिए एक HDF5 फाइल करने के लिए एक पांडा डेटा फ्रेम क्रमानुसार करने कोशिश कर रहा हूँ एचडीएफ 5 डेटासेट। – gman

+0

कोड के ऊपर टाइपो को छोड़कर बिल्कुल समान स्थिति emulates – gman

उत्तर

14

आप यूनिकोड तारों में पढ़ रहे हैं, लेकिन ASTII के रूप में अपना डेटाटाइप निर्दिष्ट कर रहे हैं। the h5py wiki के अनुसार, h5py वर्तमान में इस रूपांतरण का समर्थन नहीं करता है।

आप एक प्रारूप h5py में तार सांकेतिक शब्दों में बदलना करने की आवश्यकता होगी संभालता है:

asciiList = [n.encode("ascii", "ignore") for n in strList] 
h5File.create_dataset('xxx', (len(asciiList),1),'S10', asciiList) 

नोट: नहीं सब कुछ UTF-8 में एन्कोड किया गया ASCII में इनकोडिंग जा सकता है!

+0

धन्यवाद जो पूरी तरह से काम करता है – gman

+0

hdf5 फ़ाइल (python3 में) से इन तारों को फिर से निकालने का उचित तरीका क्या है? – DilithiumMatrix

+0

@DilithiumMatrix ASCII भी मान्य यूटीएफ -8 है, लेकिन यदि आपको 'str' प्रकार की आवश्यकता है तो आप 'ascii.decode (' utf-8 ') का उपयोग कर सकते हैं। नोट: मेरा उत्तर गैर-ASCII वर्ण छोड़ देगा। यदि आपने उन्हें 'एन्कोड (' यूनिकोड_स्केप ') के साथ संरक्षित किया है, तो आपको चीजों को वापस बदलने के लिए' डीकोड ('यूनिकोड_स्केप') की आवश्यकता है। – SlightlyCuban

1

In HDF5, data in VL format is stored as arbitrary-length vectors of a base type. In particular, strings are stored C-style in null-terminated buffers. NumPy has no native mechanism to support this. Unfortunately, this is the de facto standard for representing strings in the HDF5 C API, and in many HDF5 applications.

Thankfully, NumPy has a generic pointer type in the form of the “object” (“O”) dtype. In h5py, variable-length strings are mapped to object arrays. A small amount of metadata attached to an “O” dtype tells h5py that its contents should be converted to VL strings when stored in the file.

Existing VL strings can be read and written to with no additional effort; Python strings and fixed-length NumPy strings can be auto-converted to VL data and stored.

Example

In [27]: dt = h5py.special_dtype(vlen=str) 

In [28]: dset = h5File.create_dataset('vlen_str', (100,), dtype=dt) 

In [29]: dset[0] = 'the change of water into water vapour' 

In [30]: dset[0] 
Out[30]: 'the change of water into water vapour' 
3

मैं एक ऐसी ही स्थिति HDF5 फ़ाइल में एक डाटासेट के रूप में dataframe के स्तंभ नाम को स्टोर करने के लिए इच्छुक में हूँ। df.columns मान लिया जाये कि मैं क्या संग्रहीत करना चाहते है, मैं निम्नलिखित काम करता पाया:

h5File = h5py.File('my_file.h5','w') 
h5File['col_names'] = df.columns.values.astype('S') 

इसमें यह माना जाता स्तंभ नाम 'सरल' तार कि ASCII में एन्कोड किया जा सकता है।