Last active
October 19, 2023 19:07
-
-
Save atc0005/51cc671b02990f2c9fbfd772d87f7124 to your computer and use it in GitHub Desktop.
Python & Go | Check if lines in one file are in another file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strings" | |
) | |
func main() { | |
var userLogins int | |
usersFile, err := os.Open("users_list.txt") | |
if err != nil { | |
panic(err) | |
} | |
defer usersFile.Close() | |
loginsFile, err := os.Open("logins.txt") | |
if err != nil { | |
panic(err) | |
} | |
defer loginsFile.Close() | |
usersScanner := bufio.NewScanner(usersFile) | |
loginsScanner := bufio.NewScanner(loginsFile) | |
for usersScanner.Scan() { | |
user := strings.ToLower(usersScanner.Text()) | |
for loginsScanner.Scan() { | |
login := strings.ToLower(loginsScanner.Text()) | |
if user == login { | |
userLogins++ | |
} | |
} | |
if err := loginsScanner.Err(); err != nil { | |
panic(err) | |
} | |
} | |
if err := usersScanner.Err(); err != nil { | |
panic(err) | |
} | |
fmt.Printf("%d matching user logins\n", userLogins) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users_fh = open("users_list.txt") | |
logins_fh = open("logins.txt") | |
matching_user_logins = 0 | |
for user in users_fh: | |
user = user.strip().lower() | |
# https://stackoverflow.com/questions/34553559/python-nested-loops-only-working-on-the-first-pass | |
logins_fh.seek(0) | |
for login in logins_fh: | |
login = login.strip().lower() | |
if user == login: | |
matching_user_logins += 1 | |
users_fh.close() | |
logins_fh.close() | |
print("{} matching user logins".format(matching_user_logins)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I spun off local test code to prototype this.
Note to self: Look in the
check-if-lines-in-file-are-in-another-file
branch.