How can we in python change the number of elements in a line to the next, this is, for example...

We have the following txt file:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

And we want a program which the output is:

1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16

So how can I obtain an output file with only 3 columns instead of 4 like the original file???

Recommended Answers

All 2 Replies

Well, start with a thought. How can you explain the process to someone else in English? Make it general: given data in rows of n, how would you rewrite it in rows of m?

Once you have that explanation in English, it will lead to the thought that gives the code.

Jeff

Start with some code to read the file, as well as the file you want to write to. Also, how large is the input file? Hint, you will want probably want to use some kind of a container to hold the 3 items you want to write.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.