1

I am trying to write numeric data from a function return in the format:

[1,2,3,4,5][10,11]
[6,7,8,9,10][13,14]
...

The problem is that I want the text file to be in the format:

1,2,3,4,5[::tab::]10,11
6,7,8,9,10[::tab::]13,14
...

At the moment I am using

with open(outfilename,'a') as outfile:
    outfile.writelines("%s%s\n"%(major,minor))

I would be thankful for help please. I am still totally confused by lists, reading and writing to text files, and achieving specific formatting of data. I always end up with unwanted [] or parentheses.

1 Answer 1

1

You can simply write strings using the elements from the list, joining each element with a ,, or the separator you want; then, print the lists using a \t between them.

with open(outfilename, 'at') as outfile:

    major = ",".join(str(i) for i in major)
    minor = ",".join(str(i) for i in minor)

    outfile.writelines("%s\t%s\n" % (major, minor))

The magic behind the following line is very simple, actually:

major = ",".join(str(i) for i in major)

Python offers a feature named list comprehension, that allows you to perform an action over the elements of a list with a very clear and straightforward syntax.

For example, if you have a list l = [1, 2, 3, 4, 5], you can run over the list, converting the elements from integers to strings, calling the method str():

do _something_ with _element_ foreach _element_ in list _l_
          str(element)           for       element in l

When you do this, each resultant string will be created at a time, and you have a generator in this statement:

(str(element) for element in l)

As the method join works by iterating on a list of elements, or receiving this elements from a generator, you can have:

delimiter = ","
delimiter.join(str(element) for element in l)

Hope it clarifies the operation above.

Sign up to request clarification or add additional context in comments.

4 Comments

So simple and yet I have such difficulty with the logic of this. Thank you. Can you suggest any book or text where I can learn these logical steps. I genuinely have trouble with this. Thank you for your fantastic help.
Did this solution work for you? If so, accept the answer; I'll add an explanation on the ",".join(str(i) for i in major step, but the more I can tell you is to poke around with python using the interative python shell ipython, and consult the python documentation whenever you get in trouble: docs.python.org/2 or docs.python.org/3.
Thank you. On the same issue of reading a file in the format 1,2,3,4,5 tab 1,2. I want to return the highest value of the group with two numbers. Using split('\t')[1], I get the second group. If I use [1][0], I get the lowest number ( the data are sorted), but if I use [1][1] I get an error and if I use [1][-1], its fine with single digit numbers, but returns the second of the digits of a two digit number i.e. 10 returns 0 and 11 returns 1. I guess I still don't understand how to 'target' slicing properly
@user1478335 You are writing a file here, not reading. I guess it's better to create an appropriate question, with a description of what you're trying to do, and what you've tried so far, followed by the wrong output you're getting.

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.