。开始不起作用
在我的程序中,我有一个文件,然后我通读 for 循环中的所有行,检查每一行的开头内容。然后将每一行添加为变量。其中有 40 多行,它们几乎都是相同的,但其中一个 elif 语句没有返回 true,.startswith 不起作用。无论如何,这里是文件内容,基本上是一堆保存的信息 fN 将是我保存的变量,而 john 将是我想要的。所以这个方法就是这样做的,或者它应该是
fN:john
fP:1
fE:father email
mN:mother name
mP:1
mE:mother email @ bomg.com
a:1233 adress lane
c:city
s:state
zC:1234534
hP:(1928)phone-1123
cP:1113333
eN:emergancy
eNu:number
c1N:cluubiie 1
c1G:1st
c1B:1-23-34
c2N:clubbie 2
c2G:grade 2
c2B:birth 2
c3N:clubb 3
c3G:grade 3
c3B:birth 3
方法
def fillWindow(self,student):
global fileDirectory
location = os.path.join(fileDirectory, student + '.txt')
file = open(location, 'r')
for line in file.xreadlines():
if line.startswith('fN'):
fN = line.split(':')[1]
elif line.startswith('fP'):
fP = line.split(':')[1]
elif line.startswith('fE'):
fE = line.split(':')[1]
elif line.startswith('mN'):
mN = line.split(':')[1]
elif line.startswith('mP'):
mP = line.split(':')[1]
elif line.startswith('mE'):
mE = line.split(':')[1]
elif line.startswith('a'):
a = line.split(':')[1]
elif line.startswith('c'):
c = line.split(':')[1]
elif line.startswith('s'):
s = line.split(':')[1]
elif line.startswith('zC'):
zC = line.split(':')[1]
elif line.startswith('hP'):
hP = line.split(':')[1]
这里永远不会返回 True,即使文件中有一行以 cP 开头
elif line.startswith('cP'):
cP = line.split(':')[1]
print('True')
elif line.startswith('eN'):
eN = line.split(':')[1]
elif line.startswith('eNu'):
eNu = line.split(':')[1]
elif line.startswith('c1N'):
c1N = line.split(':')[1]
elif line.startswith('c1G'):
c1G = line.split(':')[1]
elif line.startswith('c1B'):
c1B = line.split(':')[1]
elif line.startswith('c2N'):
c2N = line.split(':')[1]
elif line.startswith('c2G'):
c2G = line.split(':')[1]
elif line.startswith('c2B'):
c2B = line.split(':')[1]
elif line.startswith('c3N'):
c3N = line.split(':')[1]
elif line.startswith('c3G'):
c3G = line.split(':')[1]
elif line.startswith('c3B'):
c3B = line.split(':')[1]
elif line.startswith('c4N'):
c4N = line.split(':')[1]
elif line.startswith('c4G'):
c4G = line.split(':')[1]
elif line.startswith('c4B'):
c4B = line.split(':')[1]
In My program I have a file and then I read through all the lines in a for loop checking each line for what it begins with. Then adding each line as a variable. There are a little over 40 lines in this and they are all pretty much the same but one of the elif statements doesnt return true the .startswith isnt working. anyways here is the file contents basically a bunch of saved information fN would be the variable I saved and john would be what I want it to be. So this method does just that or it is suppose to
fN:john
fP:1
fE:father email
mN:mother name
mP:1
mE:mother email @ bomg.com
a:1233 adress lane
c:city
s:state
zC:1234534
hP:(1928)phone-1123
cP:1113333
eN:emergancy
eNu:number
c1N:cluubiie 1
c1G:1st
c1B:1-23-34
c2N:clubbie 2
c2G:grade 2
c2B:birth 2
c3N:clubb 3
c3G:grade 3
c3B:birth 3
Method
def fillWindow(self,student):
global fileDirectory
location = os.path.join(fileDirectory, student + '.txt')
file = open(location, 'r')
for line in file.xreadlines():
if line.startswith('fN'):
fN = line.split(':')[1]
elif line.startswith('fP'):
fP = line.split(':')[1]
elif line.startswith('fE'):
fE = line.split(':')[1]
elif line.startswith('mN'):
mN = line.split(':')[1]
elif line.startswith('mP'):
mP = line.split(':')[1]
elif line.startswith('mE'):
mE = line.split(':')[1]
elif line.startswith('a'):
a = line.split(':')[1]
elif line.startswith('c'):
c = line.split(':')[1]
elif line.startswith('s'):
s = line.split(':')[1]
elif line.startswith('zC'):
zC = line.split(':')[1]
elif line.startswith('hP'):
hP = line.split(':')[1]
right here True is never returned even though in the file there is a line that starts with cP
elif line.startswith('cP'):
cP = line.split(':')[1]
print('True')
elif line.startswith('eN'):
eN = line.split(':')[1]
elif line.startswith('eNu'):
eNu = line.split(':')[1]
elif line.startswith('c1N'):
c1N = line.split(':')[1]
elif line.startswith('c1G'):
c1G = line.split(':')[1]
elif line.startswith('c1B'):
c1B = line.split(':')[1]
elif line.startswith('c2N'):
c2N = line.split(':')[1]
elif line.startswith('c2G'):
c2G = line.split(':')[1]
elif line.startswith('c2B'):
c2B = line.split(':')[1]
elif line.startswith('c3N'):
c3N = line.split(':')[1]
elif line.startswith('c3G'):
c3G = line.split(':')[1]
elif line.startswith('c3B'):
c3B = line.split(':')[1]
elif line.startswith('c4N'):
c4N = line.split(':')[1]
elif line.startswith('c4G'):
c4G = line.split(':')[1]
elif line.startswith('c4B'):
c4B = line.split(':')[1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该考虑这两行如何交互:
此外,您应该考虑使用字典来保存所有值,这样可以大大缩短程序。
You should think about how these two lines interact:
Also, you should look into using a dictionary to hold all your values, you could drastically shorten your program.
这是一段非常可怕的代码。您真的需要所有这些元素都是单独的变量吗?为什么不按照这些思路编写字典:
您只需几行即可生成它:
That's a pretty horrible chunk of code. Do you really need all those elements to be individual variables? Why not a dictionary along these lines:
which you could generate in a couple of lines only:
由于您对除第一个之外的所有替代方案都使用 elif,因此最多会评估其中一个替代方案。对于以
cP
开头的行,该条件也成立,因此不会考虑其他替代方案。
也就是说,您最好将结果存储在字典中,也许像这样:
Since you are using
elif
for all alternatives but the first one, at most one of the alternatives ever gets evaluated. For a line starting withcP
, the conditionwill also hold, so none of the further alternatives will be considered.
That said, you might be better off storing your result in a dictionary, maybe like this:
它不起作用,因为以
cP
开头的行也以c
开头,并且您将c
条件放在cP
之前>。您应该更好地重构代码以避免长 if/elif 链。这是非常难以维护的。我会使用字典,
甚至更短:(
如果你必须使用 fN 作为独立的局部变量,你可以使用,
但我不推荐它。)
It does not work because a line starts with
cP
also starts withc
, and you placed thec
condition beforecP
.You should better refactor your code to avoid the long if/elif chain. It is very unmaintainable. I would use a dictionary and
or even shorter:
(If you must use fN as an independent local variable, you could use
but I don't recommend it.)
由于名称 fN 、 fP 、 fE 、 mN 、 mP ……显然是已知且稳定的,并且由于函数定义中的 self 背叛了您想要创建实例的属性文件中读取的值,并且由于该文件具有 CSV 格式,因此我的建议是:
请注意,使用内置函数的名称命名对象(即 file)是这是一个不好的做法,并且 xreadlines() 已被弃用。
。
此外,定义
global fileDirectory
的目的是什么?语句
global x
指定,如果通过在下一行中对名称 x 进行赋值来完成对象的创建,则名称为 x< 的对象/strong> 必须被视为处于全局级别的模块中。在您的代码中,没有对 fileDirectory 进行赋值,这样会更好,因为将 fileDirectory 声明为全局可能会引发意想不到的错误。请注意,全局级别是较高级别,而不是函数的外部级别。检查以下代码:
结果
Since the names fN , fP , fE , mN, mP, .... are manifestly known and steady, and since the self in your definition of the function betrays you want to create attributes of an instance with values read in the file, and since the file has a CSV format, my proposition is to do:
Note that naming an object with the name of a built-in function ( that is to say file) is a bad practice, and that xreadlines() is deprecated.
.
Moreover, what is your intention by defining
global fileDirectory
?The statement
global x
specifies that if the creation of an object is done thanks to an assignement done to name x in the next lines, the object of name x must be considered as being in the module of global level. In your code there is no assignement to fileDirectory and it's better because declaring fileDirectory as global could provoke unsuspected errors.Note that the global level is the higher level , not the outside level of a function. Examine the following code:
result