Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with 'Find'
#1
I'm trying to re-learn python after a 15 year break (I was only ever a very new novice!).

I'm working through an old program I wrote, and - embarrassingly - I understand it all except for one section (lines 7-9).

import win32clipboard as w

 w.OpenClipboard()
    data=w.GetClipboardData()
    w.CloseClipboard()

    nullIndex=data.find('\0')
    if nullIndex != -1:
        data=data[:nullIndex]
This part of the program monitors the clipboard and checks if the data has changed from the last time it checked.

What's it looking for when it's trying to find '\0' ?

(If it said .find('\n') I would understand it was looking for the end of a line).

Thank you,

Steve
Reply
#2
In the C world a null byte \0 is used as terminator.
So that's what's a empty clipboard in Windows return.
First do this to see what it return.
>>> '\0'
'\x00'
import win32clipboard as w

w.OpenClipboard()
data = w.GetClipboardData()
print(repr(data))
w.CloseClipboard()
Before run code over clean clipbord in cmd:
cmd /c echo off | clip
Run code:
Output:
'\x00\x00\x00\x00\x00'
So the last part now will data.find('\0') not return -1,but 0.
>>> data
'\x00\x00\x00\x00\x00'

>>> nullIndex = data.find('\0')
>>> nullIndex
0
>>> # if it not -1 then this
>>> data[:nullIndex]
''
Reply
#3
Thank you. IIRC I lifted those 3 lines from someone elses code - I never understood WHY it worked at the time !

So it's saying:

Check the clipboard. If the clipboard contains 'something' then set data=0 (instead of -1) ?

Cant edit previous post
(Apr-24-2020, 10:33 AM)steve140 Wrote: Check the clipboard. If the clipboard contains 'something' then set data=0 (instead of -1) ?

I realise thats wrong.

it possibly should be

Check the clipboard. If the clipboard contains 'something' then set data= 'something [start to end]' ?
Reply
#4
It set data to and empty string '' insted of '\x00\x00\x00\x00\x00'
import win32clipboard as w

w.OpenClipboard()
data = w.GetClipboardData()
print(f'Before --> {repr(data)}')
w.CloseClipboard()

nullIndex=data.find('\0')
if nullIndex != -1:
    data=data[:nullIndex]
print(f'After --> {repr(data)}')
Output:
Before --> '\x00\x00\x00\x00\x00' After --> ''
Reply
#5
Brilliant - I understand !

Thank you very much.

When you start to learn, even the easy things are so damn complicated haha :)
Reply


Forum Jump:

User Panel Messages

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