Python Forum
Find (each) element from a list in a file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find (each) element from a list in a file (/thread-38711.html)



Find (each) element from a list in a file - tester_V - Nov-15-2022

Greetings!
I want to scan a .csv file, find cells with dates, and create a list of dates.
then I'd like to check the same file and find strings that contain the dates from the list.
I'm going to process the lines later.
Here is the code that does not work Sad
with open('C:/01/Logs_Scan_removed_prt.csv','r') as ftor :
    for el in ftor :
        el.strip()
        el=el.split(",")
        tof = el[4]
        dates.append(el[4])       
    for el in ftor :
        #print(el)    
        for ed in dates :
            print(f" ++ {ed}")
            if ed in el :
                print(f" found Line -> {el}")
Thank you.


RE: Find (each) element from a list in a file - Gribouillis - Nov-15-2022

The line el.strip() does nothing. You can remove it. Similarly the variable tof = el[4] is not used. You can remove that line too.

Which output are you expecting? Suppose that you already have the list of dates and you have a file with lines that contain zero or more of these dates. What should the output look like?


RE: Find (each) element from a list in a file - rob101 - Nov-15-2022

Simply saying "it does not work" is of no use to anyone outside of your head.

What is it that you're expecting?

It would be of help to have a sample of the data: that .csv file could be most anything.

You may want to explain the meaning behind el, ftor, ed: these mean nothing. One of the many useful features of Python, is the ability to use meaningful names for variables and as such there's no need to be so cryptic.


RE: Find (each) element from a list in a file - tester_V - Nov-15-2022

OK. got it!
Thank you!