Siamak
Posts: 70
Joined: Sat May 31, 2014 1:48 pm

How to read a word in a specific page, lane in a text document using Python

Tue Nov 28, 2017 3:29 pm

Hi Everyone,

I have been trying to use Python to read a specific word in a particular line on a given page of a "MyText.txt" file, so far with no success, is it possible?

If yes would anyone help please.
Cheers

Siamak

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to read a word in a specific page, lane in a text document using Python

Tue Nov 28, 2017 3:37 pm

How do you identify 'pages' in a .txt file?

Apart from that, the overall approach is simple:

Read and discard lines from the file until you reach the one you want.

Then parse that line using white space and punctuation symbols to pick out the required word.

If you share the code you have written so far someone may be able to suggest how to refine it or overcome problems you have.

User avatar
topguy
Posts: 6782
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to read a word in a specific page, lane in a text document using Python

Tue Nov 28, 2017 3:53 pm

How do you identify 'pages' in a .txt file?
The "old" way would be to add a Page Break.
https://en.wikipedia.org/wiki/Page_break

But still difficult to know unless .txt file is formatted by a known software.

Siamak
Posts: 70
Joined: Sat May 31, 2014 1:48 pm

Re: How to read a word in a specific page, lane in a text document using Python

Thu Nov 30, 2017 8:47 am

Hi Guys,

Thansk for the answers you have given me, I have written only 2 lines of code:
f = open("/home/pi//mytext/Test.txt", "r");
print f

IOError: [Errno 2] No such file or directory: '/home/pi//mytext/Test.txt'

I cannot go beyond this since it cannot even find the file, I tried eveything I could, no luck at all, I am not a programmer just trying something that I had in mind, thought may be asking those who know.

Any way it is not very imprtant but is good to knwo how to do it, learning is a very good thing as far as I am concerened.

Thansk again.

Siamak

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to read a word in a specific page, lane in a text document using Python

Thu Nov 30, 2017 10:16 am

Trust the system to tell you what is wrong.

Code: Select all

IOError: [Errno 2] No such file or directory: '/home/pi//mytext/Test.txt'
Try:

Code: Select all

f = open("/home/pi/mytext/Test.txt", "r")

Return to “Python”