Python 读取文本文件的下一行?
好的,我已经看了所有其他的问题,但是我不确定如何将我读到的应用到我的代码中,我也不确定它是否有效。我的文本文件如下所示:Python 读取文本文件的下一行?,python,python-3.x,line,next,Python,Python 3.x,Line,Next,好的,我已经看了所有其他的问题,但是我不确定如何将我读到的应用到我的代码中,我也不确定它是否有效。我的文本文件如下所示: NeQua,高, 伊姆科尔,中等, 尤特里,中等, 罗登,高, 我的代码能够读取第一行并剪切部分,但当我添加第二行的搜索请求时,它出现了一个错误。那么我如何让它读下一行呢 这是我目前掌握的代码: def main(): global idin,clientid print("===================") print("=Activity
NeQua,高,
伊姆科尔,中等,
尤特里,中等,
罗登,高,
我的代码能够读取第一行并剪切部分,但当我添加第二行的搜索请求时,它出现了一个错误。那么我如何让它读下一行呢
这是我目前掌握的代码:
def main():
global idin,clientid
print("===================")
print("=Activity Recorder=")
print("===================")
clientid=input("Please enter the client ID : ")
print("")
with open ("clientIntensity.txt") as search:
for line in search:
if clientid in line:
idin=line[6:]#to extract high or moderate from the text
idin=idin[:-2]
print ("Intensity = ",idin)
print("-",idin,"-")
acti()
break
elif clientid not in line:
search.next()
else:
print("Error")
main()
def acti():
global idin,clientid
if idin == "High":
print("Activites = Running, Swimming, Aerobics, Football, Tennis")
elif idin == " Moderate ":
print("Activities = Walking, Hiking, Cleaning, Skateboarding, Basketball")
else:
print("error 2")
我不太确定如何使用next函数。您不需要调用
next()
。您已经在迭代文件的行了,所以,不要做任何事情,下一行将在下一次迭代中出现
elif clientid not in line:
pass
与文件读取无关:如果elseif-else没有意义,您可以选择<第行中的code>clientid为true或false,因此有3个条件是没有意义的。只需删除中间的
elif
即可`您无需调用next()
。您已经在迭代文件的行了,所以,不要做任何事情,下一行将在下一次迭代中出现
elif clientid not in line:
pass
与文件读取无关:如果elseif-else没有意义,您可以选择<第行中的code>clientid为true或false,因此有3个条件是没有意义的。只需删除中间的
elif
就可以了`for
循环已经为您调用了下一个
由于您正在读取csv文件(一个值由逗号分隔的文件),因此您应该使用csv
模块-它会自动为您分割每一行,因此您不必自己分割每一行
为了进一步帮助您,我还删除了globals,并使用参数传递将变量传递给另一个函数。我还删除了对main()
的递归调用,并使用循环重复搜索。如果输入空字符串(只需按enter键),它将退出循环并完成程序
import csv
def main():
print("===================")
print("=Activity Recorder=")
print("===================")
while True:
clientid=input("Please enter the client ID: ")
print("")
if not clientid:
break
with open ("clientIntensity.txt") as f:
search = csv.reader(f, delimiter=',')
for row in search:
if row[0] == clientid:
idin = row[1]
print ("Intensity = ", idin)
acti(idin)
break
else:
print('ERROR: Not found')
def acti(idin):
if idin == "High":
print("Activites = Running, Swimming, Aerobics, Football, Tennis")
elif idin == "Moderate":
print("Activities = Walking, Hiking, Cleaning, Skateboarding, Basketball")
else:
print("ERROR: Unknown idin")
for
循环已经为您调用了next
由于您正在读取csv文件(一个值由逗号分隔的文件),因此您应该使用csv
模块-它会自动为您分割每一行,因此您不必自己分割每一行
为了进一步帮助您,我还删除了globals,并使用参数传递将变量传递给另一个函数。我还删除了对main()
的递归调用,并使用循环重复搜索。如果输入空字符串(只需按enter键),它将退出循环并完成程序
import csv
def main():
print("===================")
print("=Activity Recorder=")
print("===================")
while True:
clientid=input("Please enter the client ID: ")
print("")
if not clientid:
break
with open ("clientIntensity.txt") as f:
search = csv.reader(f, delimiter=',')
for row in search:
if row[0] == clientid:
idin = row[1]
print ("Intensity = ", idin)
acti(idin)
break
else:
print('ERROR: Not found')
def acti(idin):
if idin == "High":
print("Activites = Running, Swimming, Aerobics, Football, Tennis")
elif idin == "Moderate":
print("Activities = Walking, Hiking, Cleaning, Skateboarding, Basketball")
else:
print("ERROR: Unknown idin")
该行:
with open ("clientIntensity.txt") as search:
创建名为search
的文件对象。此文件对象有,但没有.next()
方法。因此,调用.next()
会抛出一个错误,其大意是“您想让我做什么?”
然而,您确实知道您想要做什么,您想要迭代每一行,并且您正在使用for循环来完成这一操作。for循环的美妙之处在于,在当前迭代结束时,它会自己继续到下一个迭代,直到到达终点(这一终点实际上是一个
StopIteration
)
因此,如果您只想继续下一行,只需让for循环滚动
with open ("clientIntensity.txt") as search:
for line in search:
if clientid in line:
idin=line[6:]#to extract high or moderate from the text
idin=idin[:-2]
print ("Intensity = ",idin)
print("-",idin,"-")
acti()
break
最后一个提示。如果您有一个字符串,例如
hello,there,
通过切片来提取there
需要花费大量的精力,但是使用string
对象的方法,即.split()
非常容易。这允许您通过指定删除器来获取子字符串列表。例如,'hello,there',.split(',')
给出['hello','there','']
。然后,您可以只使用'hello,there',.split(',)[1]
索引第二个单词,给出'there'
注意:split方法返回第三个元素的原因是字符串以逗号结尾,因此在使用逗号拆分时,也会返回结尾处的空字符串,但您可以忽略此项。行:
with open ("clientIntensity.txt") as search:
创建名为search
的文件对象。此文件对象有,但没有.next()
方法。因此,调用.next()
会抛出一个错误,其大意是“您想让我做什么?”
然而,您确实知道您想要做什么,您想要迭代每一行,并且您正在使用for循环来完成这一操作。for循环的美妙之处在于,在当前迭代结束时,它会自己继续到下一个迭代,直到到达终点(这一终点实际上是一个
StopIteration
)
因此,如果您只想继续下一行,只需让for循环滚动
with open ("clientIntensity.txt") as search:
for line in search:
if clientid in line:
idin=line[6:]#to extract high or moderate from the text
idin=idin[:-2]
print ("Intensity = ",idin)
print("-",idin,"-")
acti()
break
最后一个提示。如果您有一个字符串,例如
hello,there,
通过切片来提取there
需要花费大量的精力,但是使用string
对象的方法,即.split()
非常容易。这允许您通过指定删除器来获取子字符串列表。例如,'hello,there',.split(',')
给出['hello','there','']
。然后,您可以只使用'hello,there',.split(',)[1]
索引第二个单词,给出'there'
注意:split方法返回第三个元素的原因是字符串以逗号结尾,因此在使用逗号拆分时,也会返回结尾处的空字符串,但您可以忽略这一点。为什么从main()调用main(),但从不调用main()本身?为什么从main()调用main(),但从不调用main()本身?@RishabSudhir:谢谢。如果答案解决了问题,请接受problem@RishabSudhir:谢谢。如果答案解决了问题,请接受。非常感谢!这大大简化了我的生活@RishabSudhir没问题哦,非常感谢!这大大简化了我的生活@RishabSudhir没问题