Python Forum
How to extract a single word from a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to extract a single word from a text file
#1
Hey all. I have a test file called beta.txt which has the following data:
ARFCN: 990, Freq: 928.2M, CID: 667, LAC: 1007, MCC: 410, MNC: 3, Pwr: -30

I wish to save the value of frequency in a variable such that whenever I run the following command:

grgsm_livemon_headless -f 944.2M

The value of the frequency stored in the variable is placed in place of 944.2M.

The values of the variable may change after ever while and I want to generate a code that can keep on automatically updating the value.

Ive tried initially by reading the test file and then splitting it.
beta = open('beta.txt', 'rt')
betalines = beta.readlines()
x = print(betalines)
y = x.split()
print(y)
However this always returns the following error:
Error:
['ARFCN: 990, Freq: 928.2M, CID: 667, LAC: 1007, MCC: 410, MNC: 3, Pwr: -27\n'] Traceback (most recent call last): File "example.py", line 34, in <module> y = x.split() AttributeError: 'NoneType' object has no attribute 'split'
I then also tried to store the line in a variable but that is not working either.
mylines = []
with open ('beta.txt', 'rt') as myfile:
	for myline in myfile:
		mylines.append(myline)
print(mylines)
The list of individual words in the test file can be made by using the following code:
with open('beta.txt','r') as f:          
	for line in f:
		for word in line.split():
			print(word)
But I dont know how to proceed further so that the second word of the list is always stored in a variable, say x, and that value of x can be added in the headless livemon formula.
Thanks
Reply
#2
Your first error is because you've set "x" to be the return value of print(). print() has no return value, so "x" becomes None. What did you want x to be?

readlines() reads all the lines in the file. If you only have one line, you might prefer readline().

If you want to split the line (which was previously "betalines[0]"), you'd do betalines[0].split().

beta = open('beta.txt', 'rt')
betaline = beta.readline()
y = betaline.split()[3]
print(y)
Output:
928.2M,
Reply
#3
(Jul-21-2020, 07:02 AM)bowlofred Wrote: Your first error is because you've set "x" to be the return value of print(). print() has no return value, so "x" becomes None. What did you want x to be?

readlines() reads all the lines in the file. If you only have one line, you might prefer readline().

If you want to split the line (which was previously "betalines[0]"), you'd do betalines[0].split().

beta = open('beta.txt', 'rt')
betaline = beta.readline()
y = betaline.split()[3]
print(y)
Output:
928.2M,

Thankyou so much. I wanted to clarify one last thing though. My value is stored in the variable y. So now when I have to execute the python command:
grgsm_livemon_headless -f 944.2M
Can I simply replace the value of the frequency (in this example 944.2M) by y?
Thanks again.

edited: I tried again. the command doesnt pick up the value of y as a variable and returns the following error
Error:
grgsm_livemon_headless: error: option -f: invalid engineering notation value: 'y
Reply
#4
Is grgsm_livemon_headless a python command or a shell command? Can you show the code where you're calling it? Right now it doesn't look like python.
Reply
#5
(Jul-21-2020, 03:43 PM)bowlofred Wrote: Is grgsm_livemon_headless a python command or a shell command? Can you show the code where you're calling it? Right now it doesn't look like python.

Im new to this whole programming and i'm not sure either. This command is a part of grgsm project that is available on github.
https://github.com/ptrkrysik/gr-gsm
Reply
#6
You replaced something in your code to try to call grgsm_livemon_headless. (At least I think you did, because you're getting that error now). Can you show the code that's generating that error?
Reply
#7
(Jul-21-2020, 07:41 PM)bowlofred Wrote: You replaced something in your code to try to call grgsm_livemon_headless. (At least I think you did, because you're getting that error now). Can you show the code that's generating that error?

grgsm_livemon_headless -f y
Reply
#8
That's not a line of python. What file is that in? What else is around it?

Probably you want to run the grgsm line from python via subprocess.run. Then you can pass in the third argument as whatever you've calculated "y" to be. But I'm not sure how you're calling it now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 308 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  Searching for specific word in text files. JellyCreeper6 1 132 Nov-03-2020, 01:52 PM
Last Post: DeaD_EyE
  saving data from text file to CSV file in python having delimiter as space K11 1 243 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  How do I write a single 8-bit byte to a file? MysticLord 2 302 Sep-03-2020, 12:27 PM
Last Post: MysticLord
  Recognising mathematical expressions from word and pdf file Preeti15 0 242 Aug-19-2020, 09:06 AM
Last Post: Preeti15
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 406 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 721 Jul-17-2020, 01:45 PM
Last Post: marvel_plato
  Print output in single file using pramika loop deepakkhw 1 340 Jul-11-2020, 11:57 AM
Last Post: j.crater
  How to extract MSS (maximum size segment) from a pcap file ? salwa17 0 236 Jun-29-2020, 09:06 AM
Last Post: salwa17
  Python Speech recognition, word by word AceScottie 6 8,966 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage

Forum Jump:

User Panel Messages

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