简体   繁体   中英

How to incorporate code into a for loop using Python

Edit: Here is the code I am trying to use:

from bs4 import BeautifulSoup
import re
import sys
m = re.compile("^\d\d:\d\d$")
readfile = open("C:\\Temp\\LearnPythonTheCompletePythonProgrammingCourse_Udemy.htm", 'r').read()
soup = BeautifulSoup(readfile, "html.parser")

ci_details = soup.findAll("span",{"class":"ci-details"})

timeList = []
for detail in ci_details:
    for span in detail.findAll("span"):
        if m.match(span.text):
            timeList.append(span.text)


print (timeList)

for i in timeList:
    time1=timeList[0]
    print(time1)

edit I realized looking this over that I am telling Python to print time1 for every item in timeList. How do I iterate over timeList ?

I want to use dstubeda's code to take each entry in the list, convert it to raw seconds, add them up. Then once done, I will convert them to h:m:s. Where did I go wrong with my for loop?

It would be easier if you showed your code, but you should be able to figure it out from this:

totalTime = 0
spans = soup.find_all('span',{"class":"ci-details"})
for span in spans:
     rawTime = span.text
     processedTime = DavesTimeFunction(rawTime)
     totalTime += processedTime

print("The total time is: " + str(totalTime))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM