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]
#1
Hi,

The code below works, but it has too many lines. I tried to shorten it, but I get OSError: [Errno 36] File name too long.

What am I doing wrong?
TIA

#!/usr/bin/python3
from subprocess import check_output
import os

filename = 'tmp.txt'

# get text and save to file
def readFile():
    global filename

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

    #write to a file
    file = open(filename,'w')
    file.write(text)
    file.close()


def analyzeFile():
    readFile()
    count = 0

    # read text from file
    with open(filename, 'r') as openfile:
        for line in openfile:
            for part in line.split():
                if 'Bluetooth:' in part:
                    count += 1
    if count > 0:
        emailme(count)


def emailme(txt):
#     print(txt)
    os.remove(filename)
    pass


analyzeFile()
short version:

count = 0
text = check_output('dmesg -T', shell = True).decode('utf-8')
with open(text, 'r') as openfile:
        for line in openfile:
            for part in line.split():
                if 'Bluetooth:' in part:
                    count += 1
print(count)
Reply
#2
Is text a filename? And your code makes no sense. Why does "readFile" write a file?. In your short version why is there a file at all? Isn't "text" the text you want to process?
Reply
#3
(Mar-27-2021, 02:58 PM)deanhystad Wrote: Is text a filename?

If you mean the one on line 2 of the short version, not really. Is memory which is holding captured text.
Reply
#4
Then why are you using it as a filename?
Reply
#5
(Mar-27-2021, 03:06 PM)deanhystad Wrote: Then why are you using it as a filename?
Sad how would I treat it?
Reply
#6
In your original code you wrte "text" to a file, then you open the file and read the contents. In the short version you try to open a file using a filename that is not a filename. Why are you opening a file?

What is "text"? "It is memory" is not an answer. Everything in a program is memory. Have you tried printing it to see what it contains? Have you asked for it's type? Know the type and the contents and "how would I treat it" will be obvious.
Reply
#7
(Mar-27-2021, 03:35 PM)deanhystad Wrote: In your original code you wrte "text" to a file, then you open the file and read the contents. In the short version you try to open a file using a filename that is not a filename. Why are you opening a file?

What is "text"? "It is memory" is not an answer. Everything in a program is memory. What is the type of "text"? Know that and you'll know how to use it.

"text" is a variable filled with several lines obtained executing the command "dmesg -T" in Linux.
Reply
#8
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?
Reply
#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
#10
(Mar-27-2021, 04:02 PM)ebolisa Wrote:
(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)
but this does and not sure if it's the way to go:

import io

count = 0

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

for i in range(text.count('\n')):
    line = buf.readline()
    for part in line.split():
        if 'Bluetooth:' in part:
            count += 1

print(count)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 429 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 837 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  reading a table which is of type string saisankalpj 2 963 Dec-03-2022, 11:19 AM
Last Post: saisankalpj
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,533 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,124 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,261 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  Editing text between two string from different lines Paqqno 1 1,322 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,132 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,232 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,163 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