gpt4 book ai didi

python - 类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到文件(open() 函数)

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:23 25 4
gpt4 key购买 nike

我是编程新手,尤其是 Python。我正在尝试通过 this tutorial 来学习它.我现在在练习 19,其中一项学习练习是创建我自己设计的函数并运行它十次不同的时间。我认为到目前为止我做得很好,直到第 10 轮。这是我第一次尝试创建和写入文件,然后再次打开它以取出数字并将其放入 bread_and_butter 变量中。

错误看起来像这样:

Traceback (most recent call last):
File "ex19b.py", line 95, in <module>
j2 = open(j1)
TypeError: coercing to Unicode: need string or buffer, file found

from sys import argv

def bread_and_butter(bread, butter):
print "I have %d bread(s) and %d grams butter\n" % (bread, butter)


print "1. I can give it the numbers directly:"
bread_and_butter(10, 15)

print "2. Or I could use variables from my script:"
amount_of_bread = 10
amount_of_butter = 15

bread_and_butter(amount_of_bread, amount_of_butter)

print "3. We can even do math inside it:"
bread_and_butter(10 + 5, 7 + 9)

print "4. And we can combine both, math and variables:"
bread_and_butter(amount_of_bread + 7, amount_of_butter + 9)

print "5. We can let the user decide how much we have:"
x = raw_input("How much bread do we have?")
i = raw_input("And how much butter do we have?")

y = int(x)
z = int(i)

bread_and_butter(y, z)

print "6. Also we can take arguments for that:"

script, how_much_bread, how_much_butter = argv

q = int(how_much_bread)
w = int(how_much_butter)

bread_and_butter(q, w)

print "7. Another mathematic way:\n"
x1 = raw_input("How much breads do we have left?")
x2 = raw_input("How much breads do we need?")
y1 = raw_input("How much grams butter was left again?")
y2 = raw_input("How much grams butter do we still need?")

a1 = int(x1)
a2 = int(x2)
b1 = int(y1)
b2 = int(y2)

c, d = a2 - a1, b2 - b1

print "We still need %d bread(s) and %d gram butter but\n" % (c, d)

bread_and_butter(a1, b1)

print "8. Another way:\n"
bread = 10
butter = 300

y = bread - 8
z = butter - 1000

bread_and_butter(y, z)

print "9. Second last way:\n"

script, bread, butters = argv

f = int(bread)
g = int(butters)

l = f - 30
k = g - 500

bread_and_butter(l, k)

print "10. The last way:\n"

j = raw_input("I'll create a file with numbers in it, what's it's name?")

j1 = open(j, 'w')

number1 = raw_input("How many breads do we have?")
number2 = raw_input("And how much butter do we have?")

print "I'm going to write these to the file..."

j1.write("%s\n%s\n" % (number1, number2))

j1.close()

print "The filename is: %s." % j1.name

j2 = open(j1)
j3 = open(j1)

j4 = j2.read()
j5 = j3.read()

j6 = int(j4)
j7 = int(j5)

bread_and_butter(j6, j7)

如果有人能在这里帮助我这个菜鸟,我会很高兴。我想这不会是我不明白的最后一件事,但现在这让我感到困惑。我敢肯定,“专业人士”在看的时候就已经明白了这个问题。

无论如何,先谢谢了,

小面包

最佳答案

你得到这个错误是因为 open 正在寻找一个文件 name,而你传递给它的是文件对象本身,j1。例如,如果您改为打开 j,您将不再有错误。

编辑:如果你想打开一个文件并阅读特定的行,我会使用这样的东西:

j1 = open(j, 'w')
for i, line in enumerate(j1):
if i == 12:
# this is the 13th line
elif i == 312:
# this is the 313th line
elif i > 1000:
break
j1.close()

关于python - 类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到文件(open() 函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28723381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com