Re: [Tutor] delete strings from specificed words

2018-01-11 Thread YU Bo

Hi,
On Thu, Jan 11, 2018 at 11:40:35AM +1100, Cameron Simpson wrote:

On 09Jan2018 22:20, YU Bo  wrote:

The text i will working as follow:

```text

[...]

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index a789f952b3e9..443892dabedb 100644

[...]

+++ b/tools/perf/util/util.c

[...]

```

[...]


I have tried split(), replace(), but i have no idea to deal with it.


Do you have the text as above - a single string - or coming from a
file? I'll presume a single string.


En.., the text is  multi-string from str(something) within python.



I would treat the text as lines, particularly since the diff markers
etc are all line oriented.

So you might write something like this:

interesting = []
for line in the_text.splitlines():
  if line.startswith('diff --git '):
break
  interesting.append(line)

Now the "interesting" list has the lines you want.


Yes, i test your method  and it will come to my intend, but maybe cast
a few of other steps.

I think that the method will offer a similar ways to solve others issues.
I personally like peter's method: text.partition("diff  --git")[0].strip()

Thanks,
Bo


There's any number of variations on that you might use, but that
should get you going.

Cheers,
Cameron Simpson 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] delete strings from specificed words

2018-01-11 Thread Cameron Simpson

On 11Jan2018 12:16, YU Bo  wrote:

Hi,


Hi,


On Thu, Jan 11, 2018 at 11:40:35AM +1100, Cameron Simpson wrote:

Do you have the text as above - a single string - or coming from a
file? I'll presume a single string.


En.., the text is  multi-string from str(something) within python.


That's a single string in my thinking.


I would treat the text as lines, particularly since the diff markers
etc are all line oriented.

So you might write something like this:

interesting = []
for line in the_text.splitlines():
 if line.startswith('diff --git '):
   break
 interesting.append(line)

Now the "interesting" list has the lines you want.


Yes, i test your method  and it will come to my intend, but maybe cast
a few of other steps.

I think that the method will offer a similar ways to solve others issues.
I personally like peter's method: text.partition("diff  --git")[0].strip()


Yes, Peter's partition method is very short and direct. Personally I would 
include a leading newline in the string, thus:


 text.partition("\ndiff --git ")

to make it more precise. Consider a patch which discusses your script; its 
comment area might well mention the string "diff --git", as this email does.


What you end up with may depend on what further things you do with the text.

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] xlrd has no attribute

2018-01-11 Thread Roger Lea Scherer
I'm trying to learn how open a xls file in python. I thought this would be
easier. I've read on a blog this code and seen it on other websites. I
thought I'd try it out. I'm on Windows 10 with python 3.6.4.

import xlrd

def open_file(path):
"""
Open and read an Excel file
"""
book = xlrd.open_workbook(path)

# print number of sheets
print(book.nsheets)

# print sheet names
print(book.sheet_names())

# get the first worksheet
first_sheet = book.sheet_by_index(0)

# read a row
print(first_sheet.row_values(0))

# read a cell
cell = first_sheet.cell(0,0)
print(cell)
print(cell.value)

open_file("C:/Users/Roger/Documents/Roger/archive/area codes.xls")

But I get an error:

Traceback (most recent call last):
  File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 1, in 
import xlrd
  File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 26, in 
open_file("C:/Users/Roger/Documents/Roger/archive/area codes.xls")
  File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 7, in open_file
book = xlrd.open_workbook(path)
AttributeError: module 'xlrd' has no attribute 'open_workbook'

I've looked at http://www.lexicon.net/sjmachin/xlrd.html and it looks like
it should be ok. I've looked at Stack overflow and I have the
opening/closing parentheses appropriately placed to make it a method.

Can you help, please?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xlrd has no attribute

2018-01-11 Thread Alan Gauld via Tutor
On 11/01/18 22:52, Roger Lea Scherer wrote:

> Traceback (most recent call last):
>   File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 1, in 
> import xlrd

Look closely at the error message.
What is the name of the file that causes the error?
What is the name of the module you are importing?

You are masking the xlrd module with your xlrd.py file
and so import your own file.

Your file does not have the method so you get an error.

Don't name your applications after the modules you are using.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor