[問題] 將數字的部分複製貼上,並縮排?

看板Python作者 (我要發大財)時間3年前 (2021/11/16 22:30), 3年前編輯推噓5(509)
留言14則, 6人參與, 3年前最新討論串1/1
想問一下各位大大 https://www.cnblogs.com/hanmk/p/12747093.html 目前想要將這個網頁的程式碼,拿到我的pycharm 翻譯器做實驗 但是,整篇文章開頭都有卡數字,整篇複製貼上會變成如下: 整個排版大亂~ 15 # print(response.text) 16 return response.text 17 else: 18 return None 19 except RequestException: 20 print("请求失败") 21 # return None 22 23 24 def parse_html(html_text): 25 html = etree.HTML(html_text) 26 27 if len(html) > 0: 28 img_src = html.xpath("//img[@class='photothumb lazy']/@data-original") # 元素提取方法 29 # print(img_src) 30 return img_src 31 32 else: 33 print("解析页面元素失败") python又是很重視排版的語言,所以必須要手動一個一個刪除前面的數字 但是仍然會亂掉.... 想問一下,有沒有好一點的複製貼上方法,可以變成: def parse_html(html_text): html = etree.HTML(html_text) if len(html) > 0: img_src = html.xpath("//img[@class='photothumblazy']/@data-original") # 元素提取方法 # print(img_src) 比較符合以上快速又簡潔符合python語法的排版? 謝謝! 先貼到 NotePad++ 再轉到pycharm嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.137.155.79 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1637073023.A.77C.html

11/16 23:02, 3年前 , 1F
shift+option 上下點選刪整排數字
11/16 23:02, 1F
感謝大大,想問一下 option是? 有shift鍵但沒有option! 我使用的是windows! 非蘋果!

11/16 23:20, 3年前 , 2F
用regex findall 取代就好 ( *\d+)
11/16 23:20, 2F
※ 編輯: njpp (101.137.155.79 臺灣), 11/16/2021 23:38:01

11/17 00:11, 3年前 , 3F
windows 可以用Alt+滑鼠左鍵選垂直的前幾排刪掉
11/17 00:11, 3F

11/17 08:29, 3年前 , 4F
notepad手動幾次搜尋取代即可,練習正規
11/17 08:29, 4F

11/18 10:52, 3年前 , 5F

11/19 23:59, 3年前 , 6F
google 「pycharm column selection」就可以一次選取多行
11/19 23:59, 6F

11/19 23:59, 3年前 , 7F
的前幾個字元&一鍵刪除,notepad+貌似也有,你可google看
11/19 23:59, 7F

11/19 23:59, 3年前 , 8F
11/19 23:59, 8F

11/20 00:01, 3年前 , 9F
然後上面的regex全都不行,要是程式碼裡有數字不就很麻煩
11/20 00:01, 9F

11/20 00:01, 3年前 , 10F
,就算要用regex也是用這個「^...」,^是一行的開頭的意
11/20 00:01, 10F

11/20 00:01, 3年前 , 11F
思,使用這個regex會找到每行前三個字元,再按replaceal
11/20 00:01, 11F

11/20 00:01, 3年前 , 12F
l取代成空白即可。
11/20 00:01, 12F

11/20 02:37, 3年前 , 13F
前三字元行數破百就不行了吧
11/20 02:37, 13F

11/20 10:41, 3年前 , 14F
上面regex可以增加條件吧, ^\s*\d+
11/20 10:41, 14F
文章代碼(AID): #1Xay1_Ty (Python)
文章代碼(AID): #1Xay1_Ty (Python)