At first, I have "C:/test02.txt"

too good    positive
a film for stunt scheduling i think     objective

My codes

with open('C:/test01.txt') as words:
    ws = words.read().splitlines()

with open('C:/test02.txt') as file_modify4:
    for x in file_modify4:
        sx = map(str.strip, x.split("\t"))
        ssx = sx[0].split(" ") ## list
        for w in ssx:
            if w in ws:
                w = "" ## w...str'
            print w

After that, I remove some word and results show each word in separate line

good


film

stunt
scheduling
i
think

How I can merge them back to

good
 film  stunt scheduling i think

Edited 3 Years Ago by thanadaray

Try this:

s = '''\
good


film

stunt
scheduling
i
think
'''

mylist = s.split()
print(mylist)

s2 = mylist[0] + '\n ' + " ".join(mylist[1:])

print(s2)

Still not works. It said "Index out of range"... Any suggestion?

with open('C:/test01.txt') as words:
    ws = words.read().splitlines()
with open('C:/test02.txt') as file_modify4:
    for x in file_modify4:
        sx = map(str.strip, x.split("\t"))
        ssx = sx[0].split(" ") ## list
        for w in ssx:
            if w in ws:
                w = "" ## w...str'
            #print w

            mylist = w.split()
            print(mylist)
            s2 = mylist[0] + '\n ' + " ".join(mylist[1:])
            print(s2)

Edited 3 Years Ago by thanadaray

Here you go laddie:

with open('test01.txt') as words:
    ws = words.read().splitlines()
wds=" ".join(i for i in ws[1:] if i != '')
print (ws[0]+'\n '+wds)
This question has already been answered. Start a new discussion instead.