Skip to content

Instantly share code, notes, and snippets.

@atc0005
Last active October 19, 2023 19:07
Show Gist options
  • Save atc0005/51cc671b02990f2c9fbfd772d87f7124 to your computer and use it in GitHub Desktop.
Save atc0005/51cc671b02990f2c9fbfd772d87f7124 to your computer and use it in GitHub Desktop.
Python & Go | Check if lines in one file are in another file
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)
}
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))
@atc0005
Copy link
Author

atc0005 commented Apr 6, 2021

My goal is to reproduce in Go.

The following did not work.

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)
}

@atc0005
Copy link
Author

atc0005 commented May 6, 2021

Follow-up:

Looks like the Python script posted here suffers from the same problem: the file object iterated over in the inner loop is not reset and doesn't allow a full read more than once.

Evidently what I first tested does not match the code provided here 1:1, otherwise I would have noticed the Python error sooner.


EDIT: I've updated the original Python script to reflect the change.

@atc0005
Copy link
Author

atc0005 commented May 6, 2021

From Tim Heckman via #newbies Gophers slack

@Adam Chalkley if you intend to use it multiple times, just have the outer
loop be responsible for creating the inner loop's scanner.

That way on each iteration on the outer loop, you have a new scanner ready
to go.

It gets complex though.

A bufio.Scanner takes an io.Reader, those can only be read forward. So you'd
also need to Seek the file back to position 0. So that it can start reading
from the beginning again.

So open file, hit outer loop, seek the inner's file back to position 0,
create the scanner, invoke inner loop.

@atc0005
Copy link
Author

atc0005 commented May 6, 2021

Added Go code as a separate "file" to this Gist. I'm thinking I'd be better served by setting up a small repo for this work.

@atc0005
Copy link
Author

atc0005 commented Oct 19, 2023

Added Go code as a separate "file" to this Gist. I'm thinking I'd be better served by setting up a small repo for this work.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment