[英]Overwrite a value in a specific column with a variable in python text
我正在制作这个程序,以便用户输入一些内容,该内容存储为变量,然后用该变量覆盖文本文件中特定列中的值。 我总是遇到问题 TypeError: 'int' object is not callable 这是我需要帮助的代码。
with open("Base.txt", "r") as f:#opening it as variable f makes it easier to call to later
searchlines = f.readlines()#shortens the function f.readlines() to searchlines. This is easier to reference later
infile = open("Base.txt","r+")#opens the file in reading mode
liness = infile.readlines()#makes the variable liness to be the same as reading lines in the file
liness = ', '.join(liness)#adds commas onto liness, which is equal to new liness
liness = liness.split(" ")#breaks apart the quotations
for i, line in enumerate(searchlines):
if barcode in line:
words = line.split(" ")#sets the word variable to split the line into quotes
howmany =int(input("How many are your buying? "))
quantity=int(words[3])
if howmany < quantity:
with open("Base1.txt","w") as p:
quantity=int(words[3])
update=int(quantity-howmany)
p.write(quantity[update])
break
我的整个代码是
import sys
import string
x = 0
menu = 0
recipt = open("Recipt.txt","r+")
recipt.seek(0)
recipt.truncate()
recipt.close()
total = 0#Makes the variable total 0, this makes it easier to work with later
while x == 0:
menu = input("What do you want to do?\n 1)Add to the Recipt \n 2)View the Recipt \n 3)Checkout \n")#Gives the user a choice
if menu == "1":
recipt = open("Recipt.txt","a")#opens an empty text file. This will be the receipt
y = 0
while y == 0:
barcode = input("What is the barcode of the item? ")
if len(barcode) == 8:
searchfile = open("Base.txt", "r+")#Opens the premade database file
y = y + 1
else:
print("That barcode is not 8 in length.")
with open("Base.txt", "r") as f:#opening it as variable f makes it easier to call to later
searchlines = f.readlines()#shortens the function f.readlines() to searchlines. This is easier to reference later
infile = open("Base.txt","r+")#opens the file in reading mode
liness = infile.readlines()#makes the variable liness to be the same as reading lines in the file
liness = ', '.join(liness)#adds commas onto liness, which is equal to new liness
liness = liness.split(" ")#breaks apart the quotations
for i, line in enumerate(searchlines):
if barcode in line:
words = line.split(" ")#sets the word variable to split the line into quotes
howmany =int(input("How many are your buying? "))
quantity=int(words[3])
if howmany < quantity:
with open("Base1.txt","w") as p:
update=str(quantity-howmany)
p.write(str(quantity))
break
break
elif howmany==quantity:
update=0
p.write(str(update))
break
elif howmany>quantity:
print("We do not have that many!")
continue
line = line.replace('\n', '')#replaces line with a new line and a space
recipt.write(line + ' ' + howmany)#writes into the new file with the variable line and how many with a space in between
howmany = float(howmany)#turns the howmany variable into a float,decimal
prices = words[2]#prices is equal
prices = float(prices)
totalprice = prices * howmany
totalprice = ("{0:.2f}").format(round(totalprice,2))
recipt.write(" £" + totalprice + "\n")
total = float(total) + float(totalprice)
recipt.close()
elif barcode not in liness:
print("Item not found.")
if menu == "2":
with open("Recipt.txt","r+") as f:
for line in f:
print(line)
recipt.close()
if menu == "3":
recipt = open("Recipt.txt","a")
recipt.write("£"+str(total))
recipt.close()
sys.exit()
错误信息。
Traceback (most recent call last):
File "C:\Users\David\Desktop\Task 2 2 Version 2.py", line 37, in <module>
p.write(quantity(update))
TypeError: 'int' object is not callable
如果需要的话。 我尽量不使用 csv,所以请不要用它作为答案。 我正在使用 python 3.5。 谢谢。
您需要解决一些错误,我已对它们进行了评论。 在这两种情况下,它们都围绕您的可变quantity
。 数量是一个整数,这意味着你不能做quantity[value], quantity(value), quantity.function
quantity=int(words[3])
if howmany < quantity:
with open("Base1.txt","w") as p:
quantity=int(words[3])
update=int(quantity-howmany)
p.write(quantity[update]) #### quantity is an int, so this is the reason for your initial exception, you cannot index into quantity
break
break
elif howmany==quantity:
update=0
quantity.write(update) ### this is another exception causing bug
break
您不能对变量数量调用 .write(),因为它是一个整数
你评论说不知道回溯是什么,这是一个例子:
>>> a = 5
>>> a.write(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'write'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.