Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print only nth line
#1
FOR EXAMPLE THE FILE IS AS FOLLOWS:

[b]<td class="text-center">28,426</td>

<td class="text-center">240</td>

<td class="text-center">6,893,221</td>


Now say, I use the following code:

x=re.findall('<td\s+class\="text\-center">([0-9.,]+)</td>',x)

print(x)

which will print me all the above numerical values, but I only want to print the 2nd line what so ever the value in it maybe, how do I do it?

Thanks in advance
Reply
#2
Index the list:

x = re.findall('<td\s+class\="text\-center">([0-9.,]+)</td>',x)
print(x[2])
Reply
#3
Use a parser Cool for HTML and not regex.
from bs4 import BeautifulSoup

html = '''\
<td class="text-center">28,426</td>
<td class="text-center">240</td>
<td class="text-center">6,893,221</td>'''

soup = BeautifulSoup(html, 'lxml')
td_tag1 = soup.select('td:nth-child(2)') # Using CSS selector
td_tag2 = soup.find_all('td')
Use:
>>> td_tag1
[<td class="text-center">240</td>]
>>> td_tag1[0].text
'240'
>>> 
>>> td_tag2[1]
<td class="text-center">240</td>
>>> td_tag2[1].text
'240'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 336 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,490 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 1,190 Mar-30-2022, 04:14 AM
Last Post: DaveG
  If match not found print last line tester_V 2 2,847 Apr-26-2021, 05:18 AM
Last Post: tester_V
  print a line break in writelines() method leodavinci1990 1 6,365 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Print characters in a single line rather than one at a time hhydration 1 1,992 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 5,574 Sep-30-2020, 02:53 PM
Last Post: buran
  Pattern Require Last Line Print() why? Harshil 4 2,376 Aug-08-2020, 04:54 PM
Last Post: Harshil
  print only last matched line tester_V 24 6,324 Apr-30-2020, 05:16 AM
Last Post: deanhystad
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,895 Apr-22-2020, 02:39 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020