Find a pattern in the line of another file in python [closed]

2024/11/15 13:44:21

I'm learning python so I have two files with a lot of lines:

file 1

71528
1452
14587

file 2

country_hoj_17458   9  CA   5    CA 78.4
country_hoj_1452   10  CA   15   CA 96.5
country_hoj_14787  19  CA   51   CA 12.4
country_hoj_15742  19  CA   51   CA 12.4
country_hoj_171528  19  CA   51   CA 12.4

I want print the lines where the pattern (number) file 1 matchs with the file 2 in the first column. I want an outfile like this

 country_hoj_1452   10  CA   15   CA 96.5country_hoj_14787  19  CA   51   CA 12.4

my script is like that:

filename = numbers.txt
filename2 = data.txt
with open(filename) as f:with open (filename2) as m:for line in f:if line in m:print (m)

What I need to fix in it? Anybody can help me and support me? Thanks a lot

Answer
filename = 'numbers.txt'
filename2 = 'data.txt'with open(filename) as numberLines:with open (filename2) as dataLines:nL = numberLines.read().splitlines()dL = dataLines.read().splitlines()dataReadLines = [j for i in nL for j in dL if i in j]#dataReadLines = [i for i in nL]print (str(dataReadLines))

Another answer where each key is paired with their respective data find. I've changed your inputs and you can easily understand using the below code.

from collections import defaultdictfilename = 'numbers.txt'
filename2 = 'data.txt'with open(filename) as numberLines:with open (filename2) as dataLines:nL = numberLines.read().splitlines()dL = dataLines.read().splitlines()defDList = defaultdict(list)dataReadLines = [defDList[i].append(j) for i in nL for j in dL if i in j]#dataReadLines = [i for i in nL]print (defDList)
https://en.xdnf.cn/q/119409.html

Related Q&A

AssertionError if running code in Python prompt but not if running as file

Why trying to explain here on stackoverflow what the Python command id() does and how can it be used to reveal how Python works under the hood I had run into following strange behavior I am struggling …

Remove values before and after special character

I have a dataframe, df, where I would like to remove the values that come before the underscore _ and after the underscore _ , essentially, keeping the middle. Also keeping the digits at the end and co…

Python selection sort

Question: The code is supposed to take a file (that contains one integer value per line), print the (unsorted) integer values, sort them, and then print the sorted values.Is there anything that doesnt…

Simple inheritance issue with Django templates

just getting started in Django, and I have some problems with the inheritances. It just seems that the loop for doesnt work when inheriting other template. Heres my code in base.html:<!DOCTYPE html&…

Replacing values in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Azure Release Pipeline - Environment variables on python script

Lately Ive been requested to run a python script on my Azure Release Pipeline. This script needs some environment variables for being executed, as Ive seen that in the build pipeline, the task include …

Problem with python prepared stmt parameter passing

File C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor.py, line 1149, in execute elif len(self._prepared[parameters]) != len(params): TypeError: object of ty…

list of lists to list of tuples without loops or list comprehensions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How can I merge CSV rows that have the same value in the first cell?

This is the file: https://drive.google.com/file/d/0B5v-nJeoVouHc25wTGdqaDV1WW8/view?usp=sharingAs you can see, there are duplicates in the first column, but if I were to combine the duplicate rows, no…

i usually get this error : ValueError: invalid literal for int() with base 10

I have loaded a csv file and as i try to print it i get this error Traceback (most recent call last):File "C:\Users\FSTC\Downloads\spaceproject\main.py", line 389, in <module>world_data…