Hi guys ! I am willing to ask you if you could think of a py script that loads a text file and just joins the data contained in there following this example

This could be an input:
ssms smsmsms
sksksskkks ppppplkl
ndndndnd kdkdkdkdkppp

And this an ouput:
ssmssmsmsms
sksksskkksppppplkl
ndndndndkdkdkdkdkppp

What would basically should do is joing strings ( from the same line contained in a text file) and output a new text file with the joined texts.

Any idea would be appreciated.

The join bit can be done like this: line = ''.join(line.split()) I assume you can read and write to text files. If the inputs are not all text, it is slightly more complicated. Search for "list comprehension".

Here i use startswith('s') to get it to work on first line.
Then or to ger it to replace whitspace in everey new line \n.

This is an efficient way because you dont read hole file into memory,it work line by line.
Off course on small file this dosent matter to much.

#Open file for write
fout1 = open('join.txt', 'w')

for item in open('my_file.txt'):
    if item.startswith('s') or '\n':
        a = item.replace(' ', '')
        fout1.writelines(a)
fout1.close()

'''
join.txt
ssmssmsmsms
sksksskkksppppplkl
ndndndndkdkdkdkdkppp
'''

Edited 6 Years Ago by snippsat: n/a

snippsat have to thank you yet again :)

edit: any idea of how to join any text that has a gap between them ?

Edited 6 Years Ago by elvis1: n/a

Now it will match any text,so if your file start with asms or psms
It will do the same as my pervious post.

If there are are gap more than 1 space you mean as postet by vega,specify this better.

import re

#Open file for write
fout1 = open('join.txt', 'w')

for item in open('my_file.txt'):
    test_match = re.findall(r'.',item,re.IGNORECASE)   
    if item.startswith(str(test_match)) or '\n':
        a = item.replace(' ', '')
        fout1.writelines(a)
fout1.close()

Edited 6 Years Ago by snippsat: n/a

Do you mean a gap is more than 1 space?

sorry for not explaining better. What I need to join are brand names like Sony Ericsson into Sonyericsson, monster cable into monstercable, fujitsu siemens into fujitsusiemens and so on

edit: it would be just one space :)

Edited 6 Years Ago by elvis1: n/a

f_input = open('input.txt', 'r')
f_output = open('output.txt', 'w')

for currentline in f_input:
    newline = currentline.replace(' ', '')
    f_output.write(newline)

f_input.close()
f_output.close()

Edited 6 Years Ago by Beat_Slayer: n/a

This question has already been answered. Start a new discussion instead.