Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



   1115

Help me with my shitty code

by HornyAdmin - 27 March, 2022 - 01:54 AM
This post is by a banned member (HornyAdmin) - Unhide
83
Posts
15
Threads
4 Years of service
#1
how tf do i make a py tool that checks last 2 digits of a line in a file i provide?

with open('codes.txt', 'w') as f:
firstchar = input("18")
filedata = np.array(f)
indices = np.where(np.char.startswith(filedata, firstchar)

with open('cards_output.txt', 'w') as y:
    y.write('\n'.filedata, indices)
This post is by a banned member (UberFuck) - Unhide
UberFuck  
Godlike
1.555
Posts
375
Threads
6 Years of service
#2
(27 March, 2022 - 01:54 AM)HornyAdmin Wrote: Show More
how tf do i make a py tool that checks last 2 digits of a line in a file i provide?

Do you have an example of a couple lines that would be in the file?  Is there a reason why you are using numpy arrays?  Sorry, it's kind of hard to decipher what you are trying to accomplish.

If the line always ends in digits, you could use str(var)[-2:] to get the last two digits.  For example...
 
Code:
def example(inputFile:str):
    with open(inputFile, 'r') as f:
        for line in f:
            line = line.rstrip('\n')
            lastTwo = line[-2:]
            print(f'Last Two Digits: {lastTwo} | Full Line: {line}')

def example2(inputFile:str, firstChar:str):
    for line in filter(lambda x: x.startswith(firstChar), open(inputFile,'r').readlines()):
        line = line.rstrip('\n')
        lastTwo = line[-2:]
        print(f'Last Two Digits: {lastTwo} | Full Line: {line}')
        if lastTwo.isnumeric() and lastTwo > 10:
            # do something
            pass
This post is by a banned member (HornyAdmin) - Unhide
83
Posts
15
Threads
4 Years of service
#3
(This post was last modified: 27 March, 2022 - 12:04 PM by HornyAdmin.)
(27 March, 2022 - 10:52 AM)foxegado Wrote: Show More
(27 March, 2022 - 01:54 AM)HornyAdmin Wrote: Show More
how tf do i make a py tool that checks last 2 digits of a line in a file i provide?

Do you have an example of a couple lines that would be in the file?  Is there a reason why you are using numpy arrays?  Sorry, it's kind of hard to decipher what you are trying to accomplish.

If the line always ends in digits, you could use str(var)[-2:] to get the last two digits.  For example...
 
Code:
def example(inputFile:str):
    with open(inputFile, 'r') as f:
        for line in f:
            line = line.rstrip('\n')
            lastTwo = line[-2:]
            print(f'Last Two Digits: {lastTwo} | Full Line: {line}')

def example2(inputFile:str, firstChar:str):
    for line in filter(lambda x: x.startswith(firstChar), open(inputFile,'r').readlines()):
        line = line.rstrip('\n')
        lastTwo = line[-2:]
        print(f'Last Two Digits: {lastTwo} | Full Line: {line}')
        if lastTwo.isnumeric() and lastTwo > 10:
            # do something
            pass

its alr i fixed it :
  1.  
    Code:
    ids = []

    with open("codes.txt","r") as fi:

        for ln in fi:
            if ln.strip().endswith("16"):
                ids.append(ln)
    print(ids)

    with open('codesfiltered.txt', 'w') as output_file:
        for id in ids:
            output_file.write(id)

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)