26

I have a array of this type:

xyz = [['nameserver','panel'], ['nameserver','panel']]

How can I save this to an abc.txt file in this format:

nameserver panel
nameserver panel

I tried this using, on iterating over each row:

np.savetxt("some_i.txt",xyz[i],delimiter=',');

It's showing this error:

TypeError: Mismatch between array dtype ('<U11') and format specifier 
('%.18e')
3
  • your original list contains nameservers, but saved file -- nameserver (no s at the end) Commented Aug 15, 2018 at 14:32
  • Can you walk through your list of lists and print it to the screen? (Just trying to work out what you are having trouble with) Commented Aug 15, 2018 at 14:33
  • There's a default format speicifer: fmt='%.18e' for numpy's savetxt method, which is for savinf floating point numbers not strings. Hence the error. Commented Aug 15, 2018 at 14:46

6 Answers 6

41

This is a possible solution:

data = [['nameservers','panel'], ['nameservers','panel']]

with open("output.txt", "w") as txt_file:
    for line in data:
        txt_file.write(" ".join(line) + "\n") # works with any number of elements in a line
Sign up to request clarification or add additional context in comments.

Comments

19

Probably the simplest method is to use the json module, and convert the array to list in one step:

import json

with open('output.txt', 'w') as filehandle:
    json.dump(array.toList(), filehandle)

Advantage: Using the json format allows interoperability between many different systems.

4 Comments

Beautiful method.
the "toList" method on an array variable throws the following exception for me: *** AttributeError: 'list' object has no attribute 'toList'
this should be the first answer .. the previous answer is just too hacky and also needs lots of code checks while reading off the file while recreating it
To load: with open("output.txt","r") as fh: array=json.load(fh).
8

Use csv.writer:

import csv

data = [['nameservers','panel'], ['nameservers','panel']]

with open('tmp_file.txt', 'w') as f:
    csv.writer(f, delimiter=' ').writerows(data)

tmp_file.txt would now like this

nameservers panel
nameservers panel

Comments

4

One of many possibilities:

stuff = [['nameservers','panel'], ['nameservers','panel']]
with open("/tmp/out.txt", "w") as o:
    for line in stuff:
        print("{} {}".format(line[0], line[1]), file=o)

Comments

3

You can just write it out to a file directly.

with open('outfile.txt', 'w') as f:
    f.write('\n'.join([' '.join(l2) for l2 in l1]))

where l1 is the list you gave.

4 Comments

don't use l in variable names
makes things unreadable, especially when mixed with 1, l1ll11l1l111l1l1ll -- can you read this?
@lenik So i can't name my variable location or letter? You shouldn't use l as a single character variable name, there are no other limitations for this (PEP-8)
12 is not a variable name... @lenik ;) and can not be joined
1

Another solution could be:

# import packages
from numpy import asarray
from numpy import savetxt

# Lets say you have the data in `y`
data = asarray(y)

# Save the file
savetxt('data.csv', data, delimiter=',')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.