Hi, I'm trying to search and print any no# of Python keywords present
in a text file (say - foo.txt), and getting the above error. Sad for
not being able to decipher such a simple problem (I can come up with
other ways - but want to fix this one FFS). Any help is appreciated.
Thanks!!
import keyword, re, sys, string
inp = open("foo.txt", "r")
words,lines = 0, 0
for line in inp:
lines +=1
# a list of words
tempwords = line.split(None )
if keyword.iskeywo rd(tempwords):
print tempwords
inp.close() 7 1848
Andy <an***********@ gmail.comwrites :
Hi, I'm trying to search and print any no# of Python keywords present
in a text file (say - foo.txt), and getting the above error. Sad for
not being able to decipher such a simple problem (I can come up with
Without looking at the docs, it seems save to assume keywords.iskeyw ord would
expect a string. You pass it a list.
other ways - but want to fix this one FFS). Any help is appreciated.
Thanks!!
import keyword, re, sys, string
inp = open("foo.txt", "r")
words = sum(1 for line in inp for w in line.split() if keyword.iskeywo rd(w))
'as
for line in inp:
lines +=1
# a list of words
tempwords = line.split(None )
if keyword.iskeywo rd(tempwords):
print tempwords
You are trying here to ask if a list of words (tempwords) is a
keyword. The error is due to the implementation of the iskeyword
function which converts the keyword list into a frozenset (in which
elements must be hashable) for, I presume, performance reasons:
>>f_set = frozenset((1,2, 3,4)) ["test"] in f_set
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list objects are unhashable
What you want is something like:
for line in inp:
lines +=1
# a list of words
tempwords = line.split()
for k in tempwords:
if keyword.iskeywo rd(k):
print tempwords
Which iterates over each word in your tempwords list in turn. Note
though the following:
>>if(True):prin t"Hey!"
....
Hey!
>>s = 'if(True):print "Hey!"' s.split()
['if(True):print "Hey!"']
Which may be a problem for you if you are trying to parse badly spaced
python source files!
What you want is something like:
for line in inp:
lines +=1
# a list of words
tempwords = line.split()
for k in tempwords:
if keyword.iskeywo rd(k):
print tempwords
I think it should be:
if keyword.iskeywo rd(k):
print k
I think it should be:
>
if keyword.iskeywo rd(k):
print k
Whoops - yes of course!
>>>>"Andy" == Andy <an***********@ gmail.comwrites :
if keyword.iskeywo rd(tempwords):
print tempwords
for word in tempwords:
if keyword.iskeywo rd(word):
print word
Ganesan
--
Ganesan Rajagopal
Andy wrote:
Hi, I'm trying to search and print any no# of Python keywords present
in a text file (say - foo.txt), and getting the above error. Sad for
not being able to decipher such a simple problem (I can come up with
other ways - but want to fix this one FFS).
It helps a lot of if you post the traceback with your problem. The line
it occurred on is crucial for debugging.
But I will use my psychic debugger which tells me that the error is here:
if keyword.iskeywo rd(tempwords):
tempwords is a list. You need to iterate over the contents of the list
and run keyword.iskeywo rd() for each one. Here's an example for Python
2.5. I've cleaned up some extra lines of code that didn't have any
eventual effects in your current implementation
from __future__ import with_statement
import keyword
INFILENAME = "foo.txt"
with open(INFILENAME ) as infile:
for line in infile:
words = line.split()
for word in words:
if keyword.iskeywo rd(word):
print word
This will print multiple lines per input line. If you wanted one line
per input line then try:
with open(INFILENAME ) as infile:
for line in infile:
words = line.split()
print " ".join(word for word in words
if keyword.iskeywo rd(word))
--
Michael Hoffman This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matthew Bates |
last post by:
Hi,
I'm currently building an object-oriented shopping cart with PHP 4.3.2 and
MySQL 4.0.14.
I am looking at storing a Cart object within a PHP session, however I am not
completely sure on the best way to achieve this. I have read up and there
seems to be varying advice. Should I need to serialise and unserialise the
object or is this automatic and should I be using session_register()? For
reference, register_globals is off.
|
by: Lina Manjarres |
last post by:
Hello,
I have a form page in ASP where the first menu/list function as a filter of
the second menu list. Once I select an item form the first combo I get the
second menu full of options. This is correct. My probem is that the first
combo gets the default value again and I do not see what I did choose.
I do not know if I explained this correctly. Hope someone can understand me.
Thanks a lot, Lina
|
by: seguso |
last post by:
Hello,
For the sake of efficiency, I have chosen C++ as the language for a
"Free Adventure Construction Kit" I am developing (http://fack.sf.net).
I am looking for a way to simulate list comprehensions a-la
Haskell/Python, in order to make the logic of my code more readable.
Any way to do that would be welcome, even if this means to use macros.
Here is the code I would like to make more readable (the line marked
|
by: Crimarc |
last post by:
A list with un-sorted members
Along with a sub-list of that list with also un-sorted members
Could you tell me how to delete the sub-list members present in the
given list without first sorting their members ??
Thank you
Crimarc
|
by: gabe_pu |
last post by:
List Box Multiple Selections
I have a list box on my page and I´m trying to add new
items on it. The insert operation must be under the
selected item. When I make the insertion it is done
perfect. But the problem begins when I make the insertion
under elements that are equal (same name I mean), the
element that I want to add is inserted after the first
repeted item, even if I select for example the last
position in the ListBox....
| |
by: Little |
last post by:
I have this program and I need to work on the test portion, which tests
if a Val is in the list. It returns false no matter what could you look
at the part and see what might need to be done to fix it. It reads in
the file and sorts out the files into the four different lists.
F.txt
int main
2
" "
help
|
by: Stephen Plotnick |
last post by:
My understanding of data binding means that when a user changes a value on
the screen it actaully changes the value in an Access table.
If this is true here is what I want to do:
Threee field Access table:
First field and second field are text.
Third field is a yes/no.
|
by: sk.rasheedfarhan |
last post by:
Hi ,
Here I am new user to C#, my problem is I have to use dynamic Array of
objects. But I heard C# don't support ptrs (using managed code C#
support).
In short i initialized objects of 1000 and I am using is upto 10 or
less. Because of that I find 990 reset of them as un initialized
objects, when I extract the information from the Object it will throw
an exception also and I feel unnecessarily I am wasting of memory.
So I need dynamic...
|
by: laredotornado |
last post by:
Hi,
Using PHP 4.4.4, I have a class defined like so
class CUserItem {
var $m_id;
var $m_children_arr;
function CUserTOCItem($p_id)
{
$this->m_id = $p_id;
|
by: kiran83 |
last post by:
how can explictly delete an object in our classes(un managed objects)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |