Python Forum
Compare variable with values in a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare variable with values in a file
#1
I have a file:
cat file1.txt
cel01
cel02
cel03
cel03
I have a Oracle DB cursor that brings some values from to variable VAL_CEL1.
cursor_fgnmaes.execute("select distinct failgroup from v$asm_disk")
I want to exclude all entries found in file1.txt from cursor query , something like:
cursor_fgnmaes.execute("select distinct failgroup from v$asm_disk where failgroup not in [ALL_LINES_FROM_FILE]")
How can I do that? I tried opening file and read line per line but did not work.
Reply
#2
Wouldn't you rather ask this question in a MySQL forum??

In a MySQL query, the WHERE part needs to refer to something in a table I believe. So those file lines need to be in the table somewhere.

I never use Oracle DB, but in MySQL, you couldn't do: "select distinct failgroup from ... " You will get an error.
Either use distinct_failgroup as a column name, or distinct, failgroup as 2 column names.

Quote:SELECT distinct_failgroup FROM mytable WHERE mytable.distinct_failgroup NOT IN('cel01', 'cel02', 'cel03')

You could use Python to open the file, read each line and insert the lines as variables before running the SQL query.

with open('myfile.txt') as mf:
    mylist = mf.readlines()

my_sql = "SELECT distinct_failgroup FROM mytable WHERE mytable.distinct_failgroup NOT IN("
for i in range(mylist):
    var = mylist[i].strip() # get rid of newline
    if not i == len(mylist) -1:
        my_sql = my_sql + var + ', '
    else: 
        my_sql = my_sql + var + ')'
Then use the SQL query my_sql
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 249 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 875 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Loop through values and compare edroche3rd 6 700 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Trying to compare string values in an if statement israelsattleen 1 565 Jul-08-2023, 03:49 PM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,133 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  store all variable values into list and insert to sql_summary table mg24 3 1,164 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,696 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Overwrite values in XML file with values from another XML file Paqqno 5 3,338 Apr-01-2022, 11:33 PM
Last Post: Larz60+
  How to split file by same values from column from imported CSV file? Paqqno 5 2,806 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  How to add for loop values in variable paulo79 1 1,458 Mar-09-2022, 07:20 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020