1

I am trying to create a text file with multiple array as the columns in this file. The trick is that each array is a different datatype. For example:

a = np.zeros(100,dtype=np.int)+2 #integers all twos
b = QC_String = np.array(['NA']*100) #strings all 'NA'
c = np.ones(100,dtype=np.float)*99.9999 #floats all 99.9999

np.savetxt('filename.txt',[a,b,c],delimiter='\t')

However, I get an error:

TypeError: Mismatch between array dtype ('|S32') and format specifier   
('%.18e %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   %.18e   
%.18e')

Any ideas? Thanks!

1 Answer 1

2

I recommending using pandas to accomplish this task, which can easily handle multiple data types while writing out a new text file.

import numpy as np
import pandas as pd

# Create an empty DataFrame
df = pd.DataFrame()

# Populate columns in the dataframe with numpy arrays of different data types
df['a'] = np.zeros(100, dtype=np.int)+2
df['b'] = np.array(['NA']*100)
df['c'] = np.ones(100, dtype=np.float)*99.9999

# Store the data in a new text file
df.to_csv('./my_text_file.txt', index=False)

Opening up the .txt file reveals:

a,b,c
2,NA,99.999
2,NA,99.999
2,NA,99.999
...
Sign up to request clarification or add additional context in comments.

5 Comments

This seems great! Is there a way for it to be tab delimited instead of csv with this pandas method?
Yes, just add the sep='\t' argument during the write-out: df.to_csv('foo.txt', index=False, sep='\t')
Ok so I think it is close, however I keep getting an error: ValueError: Length of values does not match length of index
Can you edit your post to show the code that you're running that produces this error?
Ugh nevermind. I was working in Jupyter notebook and needed to restart my kernal. Thanks so much, it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.