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

Recommended Answers

All 4 Replies

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)

...

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)
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.