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


Messages In This Thread
How to extract a single word from a text file - by buttercup - Jul-21-2020, 04:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 531 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Replace a text/word in docx file using Python Devan 4 3,232 Oct-17-2023, 06:03 PM
Last Post: Devan
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 972 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,107 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  extract only text strip byte array Pir8Radio 7 2,909 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  python Multithreading on single file mg24 3 1,721 Nov-05-2022, 01:33 PM
Last Post: snippsat
  extract last word from path. mg24 4 1,732 Nov-01-2022, 10:55 AM
Last Post: carecavoador
  Extract only certain text which are needed Calli 26 5,820 Oct-10-2022, 03:58 PM
Last Post: deanhystad
  Create multiple/single csv file for each sql records mg24 6 1,380 Sep-29-2022, 08:06 AM
Last Post: buran
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,261 Jul-27-2022, 12:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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