函数len如何替换Python中的文本

时间:2018-03-21 08:07:48

标签: python python-2.7 replace

函数<?php //Import the connection include('/var/www/html/dbConnect_query_sql.php'); include('/var/www/html/dbConnect_query_mysql.php'); $mysql_projects_list = 3751, 3875, 3975, 3390; //Get the result from MSSQL $stm_mssql = $conn->prepare("SELECT P.PanelMark as Panel_Number FROM (SELECT * FROM PanelList WHERE JobNo IN ($mysql_projects_list)) P ORDER BY SSMA_TimeStamp DESC"); $stm_mssql->execute(); $result_mssql = $stm_mssql->setFetchMode(PDO::FETCH_ASSOC); $resul_panels_mssql = $stm_mssql->fetchAll(); //Get the result set from MYSQL $panel_list_mysql = $conn_mysql->prepare("SELECT PANEL_NUMBER as Panel_Number FROM PANELS ORDER BY CURRENT_DATE_TIME DESC"); $panel_list_mysql->execute(); $result_panel_list_mysql = $panel_list_mysql->setFetchMode(PDO::FETCH_ASSOC); $result_panels_mysql = $panel_list_mysql->fetchAll(); //declare variable for error checking purposes $val = 0; $val2 = 0; $val3 = 0; $panel =''; //Compare the two result sets foreach ($stm_mssql->fetchAll() as $i => $j) { foreach ($panel_list_mysql->fetchAll() as $k => $z) { //find the newly added panel if($j['Panel_Number'] != $z['Panel_Number']){ $val2++; //Create a list of panles to send in to IN Operator $mysql_new_panels[] = htmlspecialchars("'".$j['Panel_Number']."'" , ENT_NOQUOTES, 'UTF-8'); break; } else{ $val++; break; } } } if(!empty($mysql_new_panels)){ //===================================4. Get the new panel details from MSSQL and insert into the MYSQL===================================== $mysql_new_panel_list = implode(',', $mysql_new_panels); $stm_mssql = $conn->prepare("SELECT JobNo, PanelLevel ,PanelPrefix, PanelType, PanelMark as Panel_Number, ExtRef as ExtRef, SubJobNo as Sub_Job_Number, 'Status' , [SimilarTo], [Revision], [IssueForApp], [IssueForMan], [FactoryDate], [LevelCurrent] FROM PanelList WHERE PanelMark IN($mysql_new_panel_list)"); $stm_mssql->execute(); $result_mssql = $stm_mssql->setFetchMode(PDO::FETCH_ASSOC); foreach ($stm_mssql->fetchAll() as $a => $b) { //Getting the Project ID from MYSQL $mysql_project_id = $conn_mysql->prepare("SELECT PROJECT_ID, PROJECT_NUMBER, PROJECT_NAME FROM PROJECTS WHERE PROJECT_NUMBER=".$b['JobNo']); $mysql_project_id->execute(); $result_mssql = $mysql_project_id->setFetchMode(PDO::FETCH_ASSOC); $pro_id_arr = $mysql_project_id->fetchAll(); $pro_id = $pro_id_arr[0]['PROJECT_ID']; //Getting Sub Project ID from MYSQL $mysql_subproject_id = $conn_mysql->prepare("SELECT ID AS SUB_PROJECT_ID FROM SUB_PROJECTS WHERE PROJECT_ID = $pro_id AND SUB_JOB_NUMBER =".$b['Sub_Job_Number']); $mysql_subproject_id->execute(); $mysql_subproject_id->setFetchMode(PDO::FETCH_ASSOC); $sub_id_arr = $mysql_subproject_id->fetchAll(); $sub_id = $sub_id_arr[0]['SUB_PROJECT_ID']; //Check for duplicates before entering $duplicate_panel_num = $conn_mysql->prepare("SELECT PANEL_NUMBER FROM PANELS WHERE PANEL_NUMBER='".$b['Panel_Number']."'"); $duplicate_panel_num->execute(); $duplicate_panel_num->setFetchMode(PDO::FETCH_ASSOC); $panel_num_arr = $duplicate_panel_num->fetchAll(); //Check for the duplicate panel if(!$duplicate_panel_num->fetchAll()){ try{ $conn_mysql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Insert in to the MYSQL PANELS table $panel_insert = $conn_mysql->prepare("INSERT INTO PANELS (PANEL_NUMBER, PANEL_LEVEL, PREFIX, TYPE, PID, SID, SIMILAR_TO, REVISION, EXTERNAL_REFERENCE, IFA_DATE, IFM_DATE, FACTORY_DATE, SUB_JOB_NO) VALUES('".$b['Panel_Number']."', ".$b['PanelLevel'].", "."'".$b['PanelPrefix']."', '".$b['PanelType']."', ".$pro_id.", ".$sub_id.", '".$b['SimilarTo']."', '".$b['Revision']."', '".$b['ExtRef']."', '".$b['IssueForApp']."', '".$b['IssueForMan']."', '".$b['FactoryDate']."', ".$b['Sub_Job_Number'].")" ); $panel_insert->execute(); break; } catch(PDOException $e){ echo $sql . "<br> Error:" . $e->getMessage(); break; } } else{ break; } } $conn = null; $conn_mysql = null; echo "<br>COMES HERE OUT OF THE LOOP"; } else{ echo "<br>EMPTY ELSE ALL DATA EQUALS"; break; } echo"<br>LASTLY COMES HERE"; exit("<br>EXIT<br>EQUALS FOUND CONDITION: ". $val. "<br>NOT EQUAL FOUND".$val2."<br>MSSQL LOOP:".$val3); ?> 中的代码len(marker)如何删除输出中字符串replaced = line[:pos] + replacement + line[pos+len(marker):]的值,因为它用于查找字符串的长度。

帮助我理解。

marker

输出

#Example 1
marker = "AFK"
replacement = "away from keyboard"
line = "I will now go to sleep and be AFK until lunch time tomorrow."

pos = line.find(marker)
replaced = line[:pos] + replacement + line[len(marker)+pos:]

print replaced

1 个答案:

答案 0 :(得分:2)

我们希望在文本line中找到“AFK”的索引。这是通过

完成的
pos = line.find(marker)

我们看到答案是30。因此,我们将从line开始将文本从索引30开始。这是I will now go to sleep and be。然后我们会附加文字away from keyboard,然后我们会在line中附加其余文字。

因此我们将len(marker)3,因此我们会将其添加到pos的值中。结果是33。然后我们将从33索引一直到line[len(marker)+pos:]的句子结尾。

我们一起得到

replaced = line[:pos] + replacement + line[len(marker)+pos:]
  

我现在要睡觉,离开键盘直到午餐时间   明天。