I have two textfiles here. I want to count the number of occurrences of strings in “Textfile 1” among the string pairs given in “Textfile 2”.
Textfile_1:
JavaScript
x
6
1
1763_0M73
2
2610_0M63
3
7529_12M64
4
7529_18M64
5
0091_00M56
6
Textfile_2:
JavaScript
1
6
1
1763_0M73, 2610_0M63
2
2610_0M63, 7529_12M64
3
7529_18M64, 0091_00M56
4
0091_00M56, 7529_12M64
5
0267_12M64, 0091_00M56
6
Expected Output:
JavaScript
1
6
1
1763_0M73, 1
2
2610_0M63, 2
3
7529_12M64, 2
4
7529_18M64, 1
5
0091_00M56, 3
6
I tried the following script. But it is not giving the expected output.
JavaScript
1
11
11
1
with open('Textfile_2.txt') as f1:
2
lookup = dict([x.strip() for x in line.split(',')] for line in f1)
3
print(lookup)
4
5
with open('Output.txt', 'w') as out:
6
with open('Textfile_1.txt') as f2:
7
for line in f2:
8
k = line.strip()
9
n = lookup[k]
10
print(n)
11
Does anybody know how to do this in python? I’m quite new to python programming.
Advertisement
Answer
A few things not done correctly in your code. Here’s the list comprehension code.
JavaScript
1
30
30
1
#Step 1: Read the Textfile_1 and store them as dictionary values
2
#strip out the n as you read through each record from the file
3
#value of each element will be set to 0
4
with open('Textfile_1.txt','r') as f1:
5
txt1 = {_.rstrip("n"):0 for _ in f1}
6
7
#Step 2: Read the Textfile_2 and strip out the n. This will give you two values
8
#Then split the values into a list. You will get [[str1,str2],[str3,str4]...]
9
with open('Textfile_2.txt','r') as f2:
10
txt2 = [z.rstrip("n").split(',') for z in f2]
11
12
#Step 3: The strings in the list of lists may have leading or trailing spaces
13
#as you iterate thru them, remove the leading/trailing spaces
14
#then check for that value in the dictionary
15
#if found, increment the value by 1
16
for i in [y.strip() for x in txt2 for y in x]:
17
if i in txt1: txt1[i] += 1
18
19
#Step 4: print the final dictionary as it now containts the counts
20
print (txt1)
21
22
23
#Step 5: If you want to write this into a file, then use the below code
24
#Open file in write mode. Iterate thru the dictionary using txt1.items()
25
#for each key and value, write to file. Include n to have a newline
26
with open('Textfile_3.txt','w') as f3:
27
for k,v in txt1.items():
28
t3 = k + ', ' + str(v) + 'n'
29
f3.write(t3)
30
The output of this is:
JavaScript
1
2
1
{'1763_0M73': 0, '2610_0M63': 0, '7529_12M64': 2, '7529_18M64': 1, '0091_00M56': 3}
2
The output to Textfile_3
will be:
JavaScript
1
6
1
1763_00M73, 1
2
2610_00M63, 2
3
7529_12M64, 2
4
7529_18M64, 1
5
0091_00M56, 3
6