Python Forum
reading lines from a string [Solved]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading lines from a string [Solved]
#9
(Mar-27-2021, 03:54 PM)deanhystad Wrote: No. text is a string. It does not contain lines. It contains newline characters.

In the old code you wrote it to a file, read it back, and counted the number of times 'Bluetooth:' appears in the file. If the count was greater than zero, you called a function.

Can you count the number of times 'Bluetooth:' appears in a string?

If you mean some like this, it doesn't work:

count = 0

text = check_output('dmesg -T', shell = True).decode('utf-8')

nlines = text.count('\n')

for i in range(nlines):
    if text.find('Bluetooth:'):
        count += 1
print(count)
Reply


Messages In This Thread
reading lines from a string [Solved] - by ebolisa - Mar-27-2021, 02:55 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 02:58 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 03:01 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 03:06 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 03:09 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 03:37 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 03:37 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 03:54 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 04:02 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 04:35 PM
RE: reading lines from a string - by deanhystad - Mar-27-2021, 04:39 PM
RE: reading lines from a string - by ebolisa - Mar-27-2021, 05:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 461 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 879 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  reading a table which is of type string saisankalpj 2 1,003 Dec-03-2022, 11:19 AM
Last Post: saisankalpj
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,576 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,156 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,322 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  Editing text between two string from different lines Paqqno 1 1,352 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,209 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,303 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,276 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

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