#How to modify text file to 'reduce stock'

134 messages · Page 1 of 1 (latest)

young ivy
#

Could anyone help me with this?
This is the text file im working with
#Listing showing screw stock details
#MATERIAL, HEAD TYPE, LENGTH, STOCK(IN BOXES OF 50,100,200), COST PER BOX of 50 (£), DISCOUNT
brass,slot,20,35,20,10,4.00, no
brass,slot,40,40,42,40,5.00, no
brass,slot,60,55,50,30,5.50, yes
brass,star,20,30,20,10,4.00, no
brass,star,40,42,33,30,5.00, no
brass,star,60,15,20,25,5.50, no
brass,pozidriv,20,20,20,10,4.00, no
brass,pozidriv,40,8,30,30,5.00, no
brass,pozidriv,60,1,20,25,5.50, no
steel,slot,20,30,30,10,3.00, no
steel,slot,40,40,30,34,4.00, no
steel,slot,60,70,50,0,4.50, no
steel,star,20,100,47,0,3.00, no
steel,star,40,19,70,5,4.00, no
steel,star,60,90,20,20,4.50, no
steel,pozidriv,20,10,40,30,3.00, no
steel,pozidriv,40,12,30,5,4.00, no
steel,pozidriv,60,25,27,20,4.50, no

torn vault
#

Read it all into python, modify it inside python then write it all back to the file. It's not really feasible to just edit parts of files

#
with open("file.txt") as f:
    data = f.readlines()
    data[5] = "foo" # Change line 6 to say 'foo'
    
with open("file.txt", "w") as f:
    f.writelines(data)

Roughly like that

young ivy
#

this is the code i have so far so will i enter this into the loop?

#

def stock():
print('')

length = input('Enter length of the screw (20, 40, 60): ')
while length not in {'20', '40', '60'}:
    length = input('Please enter a valid length: ')
    
material = input('Enter length of the screw (brass, steel): ')
while material not in {'brass', 'steel'}:
    material = input('Please enter a valid material: ')

headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
while headtype not in {'slot', 'star', 'pozidriv'}:
    headtype = input('Please enter a valid head type: ')

for screw in screwlist:
    if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
        print(screw)
        
        amount = input('Enter how many you would like to buy (50, 100, 200): ')
        while amount not in {'50', '100', '200'}:
           amount = input('Please enter a valid amount: ')

stock()

#

i only have to modify a certain string from a row

#

#Listing showing screw stock details
#MATERIAL, HEAD TYPE, LENGTH, STOCK(IN BOXES OF 50,100,200), COST PER BOX of 50 (£), DISCOUNT
brass,slot,20,35,20,10,4.00, no
brass,slot,40,40,42,40,5.00, no
brass,slot,60,55,50,30,5.50, yes
brass,star,20,30,20,10,4.00, no
brass,star,40,42,33,30,5.00, no
brass,star,60,15,20,25,5.50, no
brass,pozidriv,20,20,20,10,4.00, no
brass,pozidriv,40,8,30,30,5.00, no
brass,pozidriv,60,1,20,25,5.50, no
steel,slot,20,30,30,10,3.00, no
steel,slot,40,40,30,34,4.00, no
steel,slot,60,70,50,0,4.50, no
steel,star,20,100,47,0,3.00, no
steel,star,40,19,70,5,4.00, no
steel,star,60,90,20,20,4.50, no
steel,pozidriv,20,10,40,30,3.00, no
steel,pozidriv,40,12,30,5,4.00, no
steel,pozidriv,60,25,27,20,4.50, no

young ivy
torn vault
#

You'd just modify the string that represents the line

#

So if you've split on "," it might be something like

data[5][0] = "steel" # change line 6 material to steel
young ivy
#

ah yes ok

torn vault
#

Since it'll then be a nested list you might need to use a loop so you can join the inner list. writelines won't do that

young ivy
#

but say if the stock is 40 how would i reduce it by 1 so that it would apply to any screw the user chooses

torn vault
#
current_stock = int(data[5][3])
current_stock -= 1
data[5][3] = f"{current_stock}"

I'd probably do somethingl ike that

young ivy
#

ok thank you

#

see the way you have [5][3] do you have to manually select this?

torn vault
#

It's just representing a 2d list

#

I think yours would just be

for screw in screwlist:
    current_stock = int(screw[3])
    current_stock += 1
    screw[3] = f"{current_stock}"
young ivy
#

so [3] would represent boxes of 50?

torn vault
#

Yeah

young ivy
#

ok i think i understand thank you

#

if amount = '50'
for screw in screwlist:
current_stock = int(screw[3])
current_stock += 1
screw[3] = f"{current_stock"}

#

could i do something like this and change it depending on the size of the box?

#

def stock():
print('')

length = input('Enter length of the screw (20, 40, 60): ')
while length not in {'20', '40', '60'}:
    length = input('Please enter a valid length: ')
    
material = input('Enter length of the screw (brass, steel): ')
while material not in {'brass', 'steel'}:
    material = input('Please enter a valid material: ')

headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
while headtype not in {'slot', 'star', 'pozidriv'}:
    headtype = input('Please enter a valid head type: ')

for screw in screwlist:
    if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
        print(screw)
        
        amount = input('Enter how many you would like to buy (50, 100, 200): ')
        while amount not in {'50', '100', '200'}:
           amount = input('Please enter a valid amount: ')

        if amount == 50:
            for screw in screwlist:
                current_stock = int(screw[3])
                current_stock -= 1
                screw[3] = {'current_stock'}

stock()

#

this is the code i have for this

torn vault
#

if amount == "50" probably

young ivy
torn vault
#

Yeah sorry it should probably be a minus. What's the error?

young ivy
#

unmatched }

#

if i put that " inside the bracket it gives me invalid syntax

torn vault
#

Can you show what you have?

young ivy
#

if amount == 50:
for screw in screwlist:
current_stock = int(screw[3])
current_stock += 1
screw[3] = f"{current_stock"}

torn vault
#

The last " should be outside the bracket

#

My mistake, sorry

#

!tip f-strings

young ivy
#

no problem

deep onyxBOT
#
f-strings

f-strings are a relatively new and simple way to format strings in python.
Available in python 3.6 and newer.
You can read more here

young ivy
#

thank u

#

no errors now but the stock doesnt change?

#

for screw in screwlist:
if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
print(screw)

        amount = input('Enter how many you would like to buy (50, 100, 200): ')
        while amount not in {'50', '100', '200'}:
           amount = input('Please enter a valid amount: ')

        if amount == '50':
            for screw in screwlist:
                current_stock = int(screw[3])
                current_stock += 1
                screw[3] = f"{current_stock}"
torn vault
#

In the file? You need to write to the file

young ivy
#

so how would i go about that i dont really understand writing to files

torn vault
#

Similar to reading but you open in write mode and use write

torn vault
#

Is this homework? Do you have any notes from class?

young ivy
#

it is but the only thing i have to go off is like videos they recorded 10 years ago so not really useful

#

dont explain it well

young ivy
torn vault
#

The last part that writes

young ivy
#

👍

#

and then i just replace data in f.writelines with screw[3] - 1 ?

torn vault
#

screwlist probably

#

||(it's not going to do what you want but do it anyway so you can see :D)||

young ivy
#

unsupported operand type(s) for -: 'list' and 'int'

torn vault
#

What's giving that error?

young ivy
#

line 83

torn vault
#

😅

#

What's line 83?

young ivy
#

so f.writelines(screwlist - 1)

#

same with f.writelines(screwlist[3] - 1)

torn vault
#

What's the -1?

young ivy
#

to reduce the stock?

torn vault
#

You did that here?

            if amount == '50':
                for screw in screwlist:
                    current_stock = int(screw[3])
                    current_stock += 1
                    screw[3] = f"{current_stock}"
young ivy
#

no clue what im doing haha

torn vault
#

Try adding some prints, it's important you can see and understand what's going on

young ivy
#

yea do i put that into the bracket of f.writelines?

torn vault
#

Randomly writing code isn't good

#

No. You put screwlist. You want to write the entire contents of it to the file

young ivy
torn vault
#

Yeah, that modifies screwlist then you want to write the contents of screwlist to the file

young ivy
#

ahhh yes i see

#

print('The total number of units in stock in the 20 length category is: ', total1)
UnboundLocalError: local variable 'total1' referenced before assignment

#

getting this when i run the code?

#

previous part of the code

torn vault
#

How do you think you might fix that?

young ivy
#

im assuming its because the stock value changes?

#

so i would need to update it?

torn vault
#

It means that you're trying to print the value of total1 without defining total1 anywhere

young ivy
#

but it worked before?

#

didnt touch that bit of code at all

torn vault
#

You'd need to share the code so I can see

young ivy
#

def showtable():
print('MATERIAL, HEAD TYPE, LENGTH, STOCK(IN BOXES OF 50,100,200), COST PER BOX of 50 (£), DISCOUNT')

for screw in screwlist:
    totalstock = (int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)
    totalvalue = (totalstock/50)*(float(screw[6]))                                                                  #    <-------     total stock divided by 50 multiplied by the cost of a box of 50?
    print(screw)
    print('The total number of units in stock is:', totalstock, 'units')
    print('The total value of the stock is: £', totalvalue, '\n')
    
for screw in screwlist:
    if screw[2].startswith('20'):
        total1=(int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)

    if screw[2].startswith('40'):
        total2=(int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)

    if screw[2].startswith('60'):
        total3=(int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)

print('The total number of units in stock in the 20 length category is: ', total1)
print('The total number of units in stock in the 40 length category is: ', total2)
print('The total number of units in stock in the 60 length category is: ', total3, '\n')

showtable()

torn vault
#

It's because it's inside an if. If the if isn't true then total1 never gets defined

#

Just do this before the loop

total1 = 0
total2 = 0
total3 = 0
young ivy
#

oh yes ok

torn vault
#

Have you looked at what got written to the file?

young ivy
#

theres nothing there

#

i have backups but not sure why its doing that

#

it doesnt display the table because its empty i get that bit

#

but nothing in text file?

torn vault
#

Share your code

young ivy
#

def stock():
print('Purchase Screws')

length = input('Enter length of the screw (20, 40, 60): ')
while length not in {'20', '40', '60'}:
    length = input('Please enter a valid length: ')
    
material = input('Enter length of the screw (brass, steel): ')
while material not in {'brass', 'steel'}:
    material = input('Please enter a valid material: ')

headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
while headtype not in {'slot', 'star', 'pozidriv'}:
    headtype = input('Please enter a valid head type: ')

for screw in screwlist:
    if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
        print(screw)

        
        
        amount = input('Enter how many you would like to buy (50, 100, 200): ')
        while amount not in {'50', '100', '200'}:
           amount = input('Please enter a valid amount: ')

        if amount == '50':
            for screw in screwlist:
                current_stock = int(screw[3])
                current_stock -= 1
                screw[3] = f"{current_stock}"
            with open("screw.txt", "w") as f:
                f.writelines(screwlist)
                print(screw)

stock()

torn vault
#

Don't do it inside the loop

#

And you'll need to put the original contents of the file back again before you run your program

young ivy
#

so like this?

#

if amount == '50':
for screw in screwlist:
current_stock = int(screw[3])
current_stock -= 1
screw[3] = f"{current_stock}"
with open("screw.txt", "w") as f:
f.writelines(screwlist)
print(screw)

#

doesnt show on thsi but its in line with if amount

torn vault
#
def stock():
    print('Purchase Screws')

    length = input('Enter length of the screw (20, 40, 60): ')
    while length not in {'20', '40', '60'}:
        length = input('Please enter a valid length: ')

    material = input('Enter length of the screw (brass, steel): ')
    while material not in {'brass', 'steel'}:
        material = input('Please enter a valid material: ')

    headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
    while headtype not in {'slot', 'star', 'pozidriv'}:
        headtype = input('Please enter a valid head type: ')

    for screw in screwlist:
        if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
            print(screw)



            amount = input('Enter how many you would like to buy (50, 100, 200): ')
            while amount not in {'50', '100', '200'}:
               amount = input('Please enter a valid amount: ')

            if amount == '50':
                for screw in screwlist:
                    current_stock = int(screw[3])
                    current_stock -= 1
                    screw[3] = f"{current_stock}"

    with open("screw.txt", "w") as f:
        f.writelines(screwlist)

stock()
#

Like that

#

Right at the end of everything

young ivy
#

ah yes ok

#

TypeError: write() argument must be str, not list for f.writelines(screwlist)

torn vault
#

Might be because of the inner lists, try just write I'm not sure how writelines is implemented internally

torn vault
young ivy
#

TypeError: write() argument must be str, not list

torn vault
#

Yeah okay you'll need a loop

young ivy
#

so for screw iun screwlist: and then with open...?

torn vault
#

Open then loop

#

You'll need to join the inner loop (screw) to make it a string then write that

young ivy
#

something like this?

#

what do you mean by join the inner loop?

torn vault
#

Inner list sorry

#

screw is a list

young ivy
#

how do i join it?

torn vault
#

Have you not come across join before?

young ivy
#

nope

torn vault
#

Okay then use another loop

young ivy
#

so what i had earlier wouldnt work?

torn vault
#

Try it

young ivy
#

same error

#

so what loop do i use

torn vault
#

You need to make screw (a list) into a string so you can write it to the file

young ivy
#

so how do i do that

torn vault
#

How do you think you'd do that?

young ivy
#

after for loop do str(screw)?

torn vault
#

Try it

#

I'm concerned I'm just telling you what to do and you don't understand what's happening or why you are getting these errors. If you're asked to explain the homework or you've done something you've not been taught it'll look suspicious

#

I'm happy to try to help but you need to try some stuff on your own

young ivy
#

same error they expect us to know what this all means because they uploaded the videos but no one understands anything but they dont really seem to care i try watching youtube videos to explain it better and some online notes but most of it isnt what were trying to do in class

#

im happy enough if you explained it and i tried it but its just so frustrating that they dont really teach us anything

#

like we havent been taught the join function as it wasnt shown in the videos so they just said we dont need it yet

torn vault
#

!eval ",".join(["foo", "bar", "baz"])

deep onyxBOT