textfileA = one word per line
textfileB = multiple words per line
textfileC = words that I have manually found already

if words from textfileA also exist in textfileB, I need to replace the words in textfileB with "error" and write the replaced words from textfileB to new lines in textfileC

Recommended Answers

All 15 Replies

Need to see some coding on your part.
What have you tried?

a = open("textfileA", "r")
b = open("textfileB", "r+")
c = open("textfileC", "a")
while 1:
    word = a.readline()
    if not word: break
    for line in b:
        if line.contains(word):
            c.write("Error: " + word + " -in-> " + line + "\n")

I decide I do not need to replace anything (I would prefer to check it manually instead, but this would help to make it all very more efficient)

textfileA and textfileB contain words (alphanumeric+decimal+space) surrounds by double quotes and seperate by comma
textfileA: "word"
textfileB: "word","word","word"

if line.contains(word):

What is this method? Are you not using standard strings? Normal python uses word in line, this looks like Ruby.

line 8 should be if word in line: ?

I am new to computer programming and I was told python is easiest

textfileB: "word","word","word"

So dos your text file look like this when you read it?

with open("textfileB.txt") as f:
    your_file = f.read()

print your_file

Can you post the output?
The reason why i ask is because it not normal to have quotes in file.
If it's quotes you have to remove them,so you can get word without quotes.

>>> your_file
'"foo","bar","hello"\n'
>>> your_file.split(',')
['"foo"', '"bar"', '"hello"\n']
>>> your_file.split(',')[0]
'"foo"'
>>> print your_file.split(',')[0]
"foo" #You see it's not the word foo, but "foo" 

Remove quotes.

>>> [i.strip().strip('"') for i in your_file.split(',')]
['foo', 'bar', 'hello']
>>> [i.strip().strip('"') for i in your_file.split(',')][0]
'foo'
>>> print [i.strip().strip('"') for i in your_file.split(',')][0]
foo

I remove characters

i, o = open('input', 'r'), open('output', 'w')
o.write(i.read().replace('"', '').replace(',', ' '))

but this still does not work, textfileA I make one line no quotes "test" and textfileB also put no quotes "test" to many lines and different places but textfileC still empty

a, b, c = open("textfileA", "r"), open("textfileB", "r+"), open("textfileC", "a")
while 1:
    word = a.readline()
    if not word: break
    for line in b:
        if word in line:
            c.write("Error: " + word + " -in-> " + line + "\n")

Ok i am not sure about your input,so i wirte something that may be what you want.
Remember you have to use close() on fileobject when you write to file or nothing gets written.
Always use close() when open/close like this open('input', 'r')
I use with open(),then is no need to close() fileobject because it's done automatic.

'''
textfileA.txt-->
bar
world

textfileB.txt-->
foo bar hello bar
'''

with open('textfileA.txt') as file_a, open('textfileB.txt') as file_b,\
open('textfileC.txt', 'w') as file_c:
    file_a = [i.strip() for i in file_a]
    file_b = file_b.read().split()
    for index,item in enumerate(file_b):
        for word in file_a:
            if word == item:
                file_b[index] = 'error'
    file_c.write(' '.join(file_b))

'''
textfileC.txt-->
foo error hello error
'''

textfileA:

21.83
34.51
53.24
43.16
79.24

textfileB:

13 x 55 58.69 4CaSJ7yBNkRkB1xw56Vk
78 x 14 53.24 EWRlAuMwBIeunoS3u34u
35 x 52 99.46 lj1WqoCwdAyV5Ne0GOow
75 x 67 32.56 ENuhVQn4Qj4rR1KAgsjc
15 x 24 14.04 RMkSoF4yfLriFn0dQqNX

textfileC:

Error: 53.24 -in-> 78 x 14 53.24 EWRlAuMwBIeunoS3u34u \n

this is just example textfileA has 170 lines textfileB has 25281 lines

I don't have textfileA.txt only textfileA
I think I may have found the bug
word = a.readline() will make '21.83\n'
so how can I remove '\n' from the end of a string word = word[:-1]

with open("textfileA") as a, open("textfileB") as b, open("textfileC", "w") as c:
    for word in a:
        if not word: break
        word = word[:-1]
        for line in b:
            if not line: break
            line = line[:-1]
            if word in line:
                c.write("Error: " + word + " -in-> " + line)

and now it can work but only for the first line in textfileA and then for line in b: never works again

>>> with open("textfileA") as a, open("textfileB") as b, open("textfileC", 'w') as c:
...     for word in a:
...         if not word: break
...         word = word[:-1]
...         print word
...         for line in b:
...             if not line: break
...             line = line[:-1]
...             if word in line:
...                 print "Error: " + word + " -in-> " + line
... 
test
Error: test -in-> 78 x 14 53.24 EWRlAtestuMwBIeunoS3u34u
Error: test -in-> 75 x 67 test 32.56 ENuhVQn4Qapplej4rR1KAgsjc
apple
test
21.83
34.51
53.24
43.16
79.24
>>> 

how can I make my for line in b: not lazy anymore?

think I solved it :)

#!/usr/bin/env python

with open("textfileA") as a, open("textfileC", 'w') as c:
    for word in a:
        if not word: break
        word = word[:-1]
        with open("textfileB") as b:
            for line in b:
                if not line: break
                line = line[:-1]
                if word in line:
                    c.write("Error: " + word + " -in-> " + line)

think I solved it :)

Yes that look good.
Can clean it up little.
word[:-1] is more common to use word.strip()

>>> s = 'bar\n'
>>> s
'bar\n'
>>> s.strip()
'bar'

So then it can look like this,remove if not word: break

with open("textfileA.txt") as a, open("textfileC.txt", 'w') as c:
    for word in a:
        word = word.strip()
        with open("textfileB.txt") as b:
            for line in b:
                line = line.strip()
                if word in line:
                    c.write('Error: {} -in-> {}'.format(word, line))
commented: thanks :) +0

thanks again ;)
I found out about the single-line if statements

if word in line: c.write("Error: {} -in-> {}".format(word, line))

then I moved .strip() so I could remove all lines that didn't end with :

with open("textfileA") as a, open("textfileC", "w") as c:
    for word in a:
        with open("textfileB") as b:
            for line in b:
                if word.strip() in line.strip(): c.write("Error: {} -in-> {}".format(word.strip(), line.strip()))

I tried to make it all on a single line but it didn't work :( I have lost my logic

with open("textfileA") as a, open("textfileC", "w") as c: for word in a: with open("textfileB") as b: for line in b: if word.strip() in line.strip(): c.write("Error: {} -in-> {}".format(word.strip(), line.strip()))

and this doesn't work either

if word.strip() as word in line.strip() as line: c.write("Error: {} -in-> {}".format(word, line))
if (word.strip() as word) in (line.strip() as line): c.write("Error: {} -in-> {}".format(word, line))

and

if (word = word.strip(); word) in (line = line.strip(); line): c.write("Error: {} -in-> {}".format(word, line))

don't work

don't work

Don't do this now code is ugly and obfuscated.
Read PEP-8,try to follow advice in PEP-8.

It can be fun to fit a lot of code into one line,this is most for fun and compositions like Golf code

(M. Fowler)
Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.

commented: I like the sound of Code Golf, can't wait to try it out and wouldn't have ever have found it if it weren't for this post :) +0
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.