hello, do you mean by this?
with open("file") as ID, open("file2") as data:
for line1.strip() in ID:
for line2.strip() in data:
if line1.strip() in line2.strip():
print(line1.strip())
?
Hi there, I have two files, file 1 looks like this :
NP_208181.1
NP_220259.1
NP_224629.1
WP_232131
WP_3432434
WP_2441241221
File 2 looks like this:
NP_208181.1,GCF_000008525.1
NP_212206.1,GCF_000008685.2
NP_213866.1,GCF_000008625.1
NP_219784.1,GCF_000008725.1
NP_220151.1,GCF_000008725.1
NP_220259.1,GCF_000008725.1
NP_224628.1,GCF_000008745.1
NP_224629.1,GCF_000008745.1
NP_224939.1,GCF_000008745.1
My purpose is to find which ID in file 1 is in file 2 too, so here we can see NP_208181.1, NP_220259.1, NP_224629.1 can be found in file two, followed by GCF blabla, i wrote a small script like this :
import re
with open("file1") as ID, open("file2") as data:
for line1, line2 in zip(ID,data):
if line1 in line2:
print(line1)
However, the result was blank, which does not make sense, any one knows why? how t modify this script?
First, you need to strip eol from the lines. For example line1.strip(). Second, with zip, you are testing line against corresponding line. This should catch the first line but none of the others.
hello, do you mean by this?
with open("file") as ID, open("file2") as data:
for line1.strip() in ID:
for line2.strip() in data:
if line1.strip() in line2.strip():
print(line1.strip())
?
Combining my comment and jomo018s point about the line ending character (line stripping is only necessary from file 1 since the strings are contained within the line of file 2, but I've done both here anyway):
#!/bin/python
# assume the script is named comparelines.py
# invoke with the ID file as the first commandline arg,
# and data file as commandline arg 2
import sys
with open(sys.argv[1], 'r') as ID_file, open(sys.argv[2], 'r') as data_file:
IDs = [ID.strip() for ID in ID_file]
data = [line.strip() for line in data_file]
result = [j for i in IDs for j in data if i in j]
for each in result:
print(each)
So
$ python comparelines.py IDs.txt data.txt
gives:
NP_208181.1,GCF_000008525.1
NP_220259.1,GCF_000008725.1
NP_224629.1,GCF_000008745.1
EDIT
Fixed it.
I believe all your input file are actually csv file
thus, the most efficient way is: 1) read these file into dataframe 2) inner join the column you want
Log in to answer this question.
Without testing it I think you're zipping the 2 lines together from each file, so it's only comparing line1 in file 1 with line 2 in file 2, then line 2 with line 2 etc You'll need 2 loops for this to work as you've got it - e.g:
and so on..
I'd look in to using the
anyandallpython keywords though, they may help here.If you're not bothered about using python specifically, you could do this in a single line (sort of) with
grep:Hi, thanks for correction, but I tried , still blank, here is my new code:
I believe this answer is important: A: Technical question about python "to find the strings"
what about comm?
Fixed some duff logic in my answer, it should work for your case now.