需要整数吗?打开() [英] An integer is required? open()
问题描述
我有一个非常简单的 Python 脚本,它应该扫描一个文本文件,该文件包含格式为 id='value' 的行并放入他们进入一个字典.python 模块名为 chval.py,输入文件为 in.txt.这是代码:
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:
回溯(最近一次调用最后一次):
文件chval.py",第 9 行,在 ?f = open(sys.argv[1], 'r') 类型错误:需要一个整数
Traceback (most recent call last):
File "chval.py", line 9, in ? f = open(sys.argv[1], 'r') TypeError: an integer is required
我正在使用 python 2.4...因为我被要求不使用任何更新的东西,有没有我不知道的关于 open() 的东西?为什么它需要一个整数?
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?
推荐答案
因为你做了 from os import *
,你(偶然)使用了 os.open,它确实需要一个整数标志而不是文本r"或w".去掉那条线,你就会克服那个错误.
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屋!