Home > Software design >  np.savetxt tab delimited txt file
np.savetxt tab delimited txt file

Time:12-29

I have a dataframe that needs be txt file. So, I use np.savetxt method. However, dataframe consists of four columns which are string, string, string and integer, respectively.

np.savetxt takes fmt and delimiter parameters. In official document, it is said that multi-format string, e.g. ‘Iteration %d – .5f’, in which case delimiter is ignored

How can I use fmt for both specifying the columns' data types and for tab delimiter?


fmt = ('%s %s %s %d')

np.savetxt("test.txt", df, fmt=fmt)

Sample data:

DTL 3E046ZM/A 190198848376 0
DTL 3E047ZM/A 190198848406 0
DTL 3E309ZM/A 190198937339 0

I tried ('%s \t %s \t %s \t %d') but it didn't work.

CodePudding user response:

As numpy.savetxt docs informs you, you might provide sequence of formats e.g. list of strs as fmt, consider following simple example

import numpy as np
import pandas as pd
df = pd.DataFrame([[1,2,3,"Hello"],[4,5,6,"World"]])
fmt = ["%d","%d","%d","%s"]
np.savetxt("test.tsv", df, fmt=fmt, delimiter="\t")

creates test.tsv with following content

1   2   3   Hello
4   5   6   World

CodePudding user response:

You said you use dataframes, so pandas is is involved right?

Why then not just use it directly?:

df.to_csv('your_file.txt', header=None, index=None, sep='\t', mode='a')

this will format the output according to the datatype the columns had in the dataframe.

  • Related