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
#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


Messages In This Thread
RE: How to extract a single word from a text file - by bowlofred - Jul-21-2020, 07:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove unwanted images and tables from a Word file using Python? rownong 2 952 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,221 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Extract and rename a file from an Archive tester_V 4 4,189 Jul-08-2024, 07:54 AM
Last Post: tester_V
  Extract text from PDF goryee 2 9,640 Jul-08-2024, 06:35 AM
Last Post: Pedroski55
  extract substring from a string before a word !! evilcode1 3 2,078 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Replace a text/word in docx file using Python Devan 4 25,379 Oct-17-2023, 06:03 PM
Last Post: Devan
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 4,530 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 2,151 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  extract only text strip byte array Pir8Radio 7 7,525 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  python Multithreading on single file mg24 3 3,666 Nov-05-2022, 01:33 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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