multiple lines in one line

DL Neil PythonList at DancesWithMice.info
Fri Aug 30 17:14:04 EDT 2019


On 31/08/19 8:26 AM, tascioglu.tansu at gmail.com wrote:
> Hello,I working on some file operations with python.I have 2 text file which is consist of lines such as apple_pie 0.3434 0.6767 0.2312 and other text file has apple 0.2334 0.3412 0.123
> pie 0.976 0.75654 0.2312
> I want to append lines like apple_pie 0.3434 0.6767 0.2312 0.2334 0.3412 0.123(values for apple) 0.976 0.75654 0.2312(values for pie) in one line. does anybody know how to do it ?Thanks...


This is a very broad question. Is it a homework assignment?


First find a consistent relationship between the three words: 
"apple_pie", "apple", and "pie" - is the first always a combination of 
the others? ... always separated by an under-score/under-line? ...always 
in the same sequence of words?

Are the two files sorted in a 1:1 relationship, ie so that the 
nth-record from one 'matches' the nth-record from the other - or is 
record-matching part of the problem?


To solve any ComSc problem think about how it can be split into 
sub-problems. The analogy that is often used, talks about peeling-back 
the successive layers of an onion. Reduce the size of the problem being 
considered, until each sub-problem is small-enough for you to 
(understand how to) solve.

Below, are several functions addressing possible sub-problems. Solving 
one sub-problem can then feed its 'answer' into solving a 'larger' 
problem...


Possibly useful functions:

- take a string and split it into 'first word' and 'rest of line'

- take a two-word string and split it into the two separate words

- take a two-part record and given the two independent words, split it, 
and return its two components

(plus, building sub-problem solutions as Python functions enables us to 
thoroughly test each function separately - to ensure it 'does the job', 
before moving-on to the next, ie before things become ever-more complex 
and testing 'the whole thing' reveals an error without giving ideas as 
to 'when/where' things went-wrong!)


If you come back to us, please add more data examples (otherwise the 
answer becomes: find( "apple" )!), plus sample code showing the progress 
you've made to-date...
-- 
Regards =dn



More information about the Python-list mailing list