一个整数是必需的?打开() [英] An integer is required? open()
问题描述
我有一个非常简单的python脚本,它应该扫描一个文本文件,其中包含格式为 id =' value '的行,他们成为一个字典。 python模块叫做chval.py,输入文件是in.txt。这里是代码: 当我尝试运行它时我得到: 追溯(最近一次调用最后一次): 使用Python 2.4 ...因为我已经被挑战不使用任何新东西,有什么关于open(),我不知道?为什么它需要一个整数? 什么后面的行是未经测试的。简而言之:为什么它给了我错误,我该如何解决这个问题? 解决方案
$ pre $ 从os导入os,sys
*从sys导入
导入*
vals = {}
f = open(sys.argv [1],'r')
为val_f中的行:
t = line.split('=')
t [1] .strip('\'')
vals.append(t [0],t [1])$ b
$ b print vals
f.close()
$ b
文件chval.py,第9行,在?
f = open(sys.argv [1],'r')TypeError:需要一个整数
>从os导入* ,你(意外地)使用os.open,这确实需要一个整数标志而不是文本r或w。拿出那条线,你会得到这个错误。
I have a very simple python script that should scan a text file, which contains lines formatted as id='value' and put them into a dict. the python module is called chval.py and the input file is in.txt. here's the code:
import os,sys
from os import *
from sys import *
vals = {}
f = open(sys.argv[1], 'r')
for line in val_f:
t = line.split('=')
t[1].strip('\'')
vals.append(t[0], t[1])
print vals
f.close()
when i try to run it i get:
Traceback (most recent call last):
File "chval.py", line 9, in ? f = open(sys.argv[1], 'r') TypeError: an integer is required
I'm using python 2.4... because i've been challenged to not use anything newer, is there something about open() that I don't know about? Why does it want an integer?
anything after that line is untested. in short: why is it giving me the error and how do i fix it?
Because you did from os import *
, you are (accidenally) using os.open, which indeed requires an integer flag instead of a textual "r" or "w". Take out that line and you'll get past that error.
这篇关于一个整数是必需的?打开()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!