我正在尝试学习几种语言,因此我在不同的语言上也会遇到同样的问题。这是我的代码:
def read_one_file():
with open('C:\Python27\inventory.dat', 'r') as f:
invid = f.readline().strip()
invtype = f.readline().strip()
price = f.readline().strip()
stock = f.readline().strip()
title = f.readline().strip()
author = f.readline().strip()
published = f.readline().strip()
return invid, invtype, price, stock, title, author, published
def write_one_file(holdId, holdType, holdPrice, holdStock, holdTitle, holdAuthor, holdPublished):
with open('C:\Python27\inventory.dat', 'w') as f:
invid = holdId
price = holdPrice
newstock = holdStock
published = holdPublished
f.write("Item Id: %s\n" %invid)
f.write("Item Type: %s\n" %holdType)
f.write("Item Price: %s\n" %price)
f.write("Number In Stock: %s\n" %newstock)
f.write("Title: %s\n" %holdTitle)
f.write("Author: %s\n" %holdAuthor)
f.write("Published: %s\n" %holdPublished)
return
invid, invtype, price, stock, title, author, published = read_one_file()
print "Update Number In Stock"
print "----------------------"
print "Item ID: ", invid
print "Item Type: ", invtype
print "Price: ", price
print "Number In Stock: ", stock
print "Title: ", title
print "Author/Artist: ", author
print "Published: ", published
print "----------------------"
print "Please Enter New Stock Number: "
newstock = raw_input(">")
write_one_file(invid, invtype, price, newstock, title, author, published)
编辑:我尝试使用str()进行转换,但仍然无法运行。
EDIT2:我最终将%d更改为%s并且似乎有效。唯一的问题是当我运行它时,它倾向于将书放在123456上。
控制台中最终会发生什么
Update Number In Stock
----------------------
Item ID: 123456book
Item Type: 69.99
Price: 20
Number In Stock:
Title: Walter_Savitch
Author/Artist: 2011
Published:
----------------------
Please Enter New Stock Number:
>
这是.txt文件:
123456book 69.99 20
Walter_Savitch 2011
带换行符的东西?
答案 0 :(得分:5)
f.write()
期望一个字符串作为参数。您将invid
,invtype
等定义为tuples,它们不会自动转换为字符串。您可以使用str()
函数显式转换它们,或者您更喜欢使用某些字符串格式,例如:
"Number in stock%d"%nrStock
其中"%d"表示nrStock是一个整数。
您注意到我已将您的d
变量重命名为nrStock
。使用描述性变量名称通常是一种好习惯,或者更好的想法可能是使用dictionary。
答案 1 :(得分:2)
文件句柄需要一个字符串对象,并且你给它一个元组对象。
import os
def write_one_file(a, b, c, d, e, f, g, fname=r'C:\Python27\inventory.dat'):
with open(fname, 'w') as f:
f.write('Item ID: {}{}'.format(a, os.linesep))
# etc..
请注意,您的函数中不需要return语句,因为您没有返回任何内容。
答案 2 :(得分:2)
write
方法将一个字符串作为参数。您可以使用以下任何方式使用数字变量格式化字符串:
f.write("Value :%d" % number)
f.write("Value :%s" % number)
f.write("Value :" + str(number))
f.write("Value :{}".format(number))
如果要插入的变量是字符串,请尝试以下任何一种:
f.write("Value :%s" % string)
f.write("Value :" + string)
f.write("Value :{}".format(string))
请注意,“%d”格式说明符用于以十进制格式插入数字。如果您提供字符串,它将导致异常。
有关%运算符的说明,请参阅此处:http://docs.python.org/library/stdtypes.html#string-formatting
有关字符串的format
方法的说明,请参阅此处:
http://docs.python.org/library/string.html#formatstrings
通常首选格式化方法。
答案 3 :(得分:1)
您现在似乎无法阅读文件。它似乎不是您认为的格式。
我假设这是你文件的内容,当你把它放在你的问题中时,Stackoverflow会把它弄错一点:
123456book
69.99
20
Walter_Savitch
2011
如果您的文件在“123456”和“book”之间确实没有换行符,那么将其作为readline
的一行读取就不足为奇了。这个文件应该是那样的吗?您是否专门为此目的创建了它,还是从其他地方以您无法控制的格式获取?此代码是否需要与其他类似文件一起使用?如果是这样,他们有什么布局?如果没有更多信息,很难准确猜出文件的常见布局是什么。
您可以编辑文件以在“123456”和“book”之间添加换行符,以使其与您的程序所期望的格式相匹配。
如果您的第一行的格式保证是六位数ID,后跟任意文本,您可以将其拆分为:
idline = f.readline().strip()
idnumber = idline[:6]
idtype = idline[6:]
# Now idnumber == "123456"
# and idtype == "book"
请在此处查看“切片表示法”的说明:http://docs.python.org/tutorial/introduction.html#strings
如果您的第一行的格式保证是任意数量的数字,后跟任意数量的小写字母,您可以使用正则表达式将其拆分:
import re
...
idline = f.readline().strip()
idnumber, idtype = re.match("([0-9]*)([a-z]*)", idline).groups()