本文介绍了Python for循环语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for循环中似乎存在语法错误,但我不知道为什么。我是Python的新手,我正在Centos中尝试此操作。

There seems to be a syntax error at the for loop but I don't know why. I'm somewhat new to Python, and I'm trying this in Centos.

fil = open('acnt_data_text.txt', 'r')
for data_str in fil:
    result = subprocess.call(["php", "class.php"], data_str)

print result

fil.close()

语法错误:无效的语法:对于fil中的data_str:

推荐答案

我认为应该这样隔开

fil = open('acnt_data_text.txt', 'r')
for data_str in fil:
   result = subprocess.call(["php", "class.php"], data_str)
print result

fil.close()

您可以如果您使用 with 而不是像这样

You can avoid closing the file if you use with instead like so

with open('acnt_data_text.txt', 'r') as fil:
    for data_str in fil:
       result = subprocess.call(["php", "class.php"], data_str)
    print result

这篇关于Python for循环语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 18:24