2,646 Posted Topics
![]() | Re: `os.system()` is a (deprecated) function to run another program from your python program. Changing your process' working directory is different from starting another program. Use `os.chdir(path)` to change the current working directory and `os.getcwd()` to get the current working directory. |
Re: A known solution is import codecs file = codecs.open(path, encoding='iso8859-1') see if it works for you. | |
Re: I have these links [url]http://www.iconlet.com/browse[/url] [url]http://www.daniweb.com/forums/thread12398.html[/url] | |
![]() | Re: Did you read the sticky thread [projects-for-the-beginner](http://www.daniweb.com/software-development/python/threads/32007/projects-for-the-beginner) which contains many ideas in this direction ? ![]() |
Re: Use recursion: write a function F(dictionary) to write a dictionary. In the function's body, iterate over the dictionary items, and if one of the item contains a new dictionary, call F(subdictionary) again to write this dict. | |
![]() | Re: I once wrote a small class to help beginners write text based games, [see here](http://www.daniweb.com/software-development/python/code/217215/a-class-for-menu-based-terminal-applications). See if it can help you (you can replace Print with print in the code and remove the cbprint import). |
Re: Try this [code=python] # python >= 2.5 def isLeapYear(year): return False if year % 4 else True if year % 100 else not year % 400 [/code] :) | |
Re: All this would be more serious if you could describe the exact syntax *rules* of the input string. | |
Re: Yes, your statements are correct. Here is how you could implement the cpp static field behavior in python using an accessor function instead of an attribute from __future__ import print_function from functools import partial # preliminary definitions _default = object() def _helper_static_cpp(container, value = _default): if value is _default: return … | |
Re: > I have many single float but how I can make then to be list of float? It's fairly easy. At the top of your program, you add `thelist = list()`. Then when you want to add a score to the list, you write `thelist.append(score)`. Finally, add `print(thelist)` at the … | |
Re: Probably for row in c: print (row) | |
This code snippet provides methods to convert between various ieee754 floating point numbers format. For example double precision to single precision. The format is given by a pair (w, p) giving the size in bits of the exponent part and the significand part in the ieee754 representation of a number … | |
Re: The best algorithm is gaussian elimination. Read this first [Click Here](http://en.wikipedia.org/wiki/Gaussian_elimination) | |
Re: I suggest to write it as while((tr < row) && (grid[tr][tc]=='0'||grid[tr][tc]==s[i]) && (count<strlen(s))) { ++count; ++tr; } If `tr < row` fails, `grid[tr]` will not be computed :) | |
Re: Here is a way using my snippet on [non recursive tree traversal 1.4](http://www.daniweb.com/software-development/python/code/395270/generic-non-recursive-tree-traversal) from functools import partial import walktree as wt def subn(letters, k, node): if len(node) == k: return for x in letters: yield node + x for path in wt.walk("", partial(subn, "ABC", 3), wt.event(wt.leaf)): print path[-1] """my output … | |
Re: Here is a working version with a varying 'guessrange' :) # CPU Guess My Number # # You pick a random number between 1 and 100 # The computer tries to guess it in the fewest attempts # automatically saying if the number is too high or low # or … | |
Re: Here is one way to do it from operator import itemgetter import itertools as itt valuearray = [['A', '21', '45'], ['A', '12', '23'], ['A', '54', '21'], ['A', '15', '54'], ['B', '23', '53'], ['B', '34', '53'], ['B', '32', '54'], ['B', '24', '13'], ['C', '31', '43'], ['C', '42', '54'], ['C', '35', '54'], … | |
Re: Interesting, but why not use module fnmatch ? | |
Re: This is a typical use case for the `__new__()` method: class BalancedTernary(long): def __new__(cls, n): instance = long.__new__(cls, balanced_ternary_value(n) if isinstance(n, str) else n) return instance def __repr__(self): return make_balanced(self) __str__ = __repr__ Your code seems to work now. | |
Re: There is no such rule. `import myScript` reads the file on the disk if there is no module in `sys.modules['myScript']`. This module may exist even if the name myScript is not defined in the current global namespace. Apparently, your app closes and reopens the shell window in the same python … | |
Re: It seems that the signature of Insert is `Insert(self, item, pos, client_data=None)`, [See here](http://www.wxpython.org/docs/api/wx.ListBox-class.html#Insert) . | |
Re: You can use sub() with a method as argument import re from functools import partial repl_dict = {'cat': 'Garfield', 'dog': 'Oddie' } def helper(dic, match): word = match.group(0) return dic.get(word, word) word_re = re.compile(r'\b[a-zA-Z]+\b') text = "dog ate the catfood and went to cat's bed to see dog dreams on … | |
Re: You are making an uncontroled use of recursion: `Get_num()` calls `Get_num()`, etc. Each function must have a precise task to do, which can be described in a short documentation string. Look at the following pseudo code for example # THIS IS PSEUDO CODE, NOT REAL CODE def main_job(): """Repeatedly ask … | |
| |
Re: Normally in windows, if you click on a .py file, it runs the file in a cmd console. If you want to run the program without console, you can rename it with the .pyw extension. | |
Re: You will probably find a way to plot the data in the [matplotlib gallery](http://matplotlib.sourceforge.net/gallery.html) . Each picture comes with its source code. | |
Re: Here is how you can find which variables are None [code=python] missing_list = [ i for (i, z) in enumerate((v, u, a, t)) if z is None ] [/code] This returns a sublist of [0, 1, 2, 3] with the indexes of the variables equal to None. | |
Re: As your data file looks like xml, I obtained some results very easily using module xml.parsers.expat. Here is the code. It uses this code snippet [i]adaptstrings[/i] [url]http://www.daniweb.com/software-development/python/code/389929[/url] (this could be optimized later). [code=python] from itertools import chain from adaptstrings import adapt_as_file import xml.parsers.expat PREFIX = "<document>\n" SUFFIX = "</document>\n" def … | |
Re: When you call [icode]character.useItem(item)[/icode] you want the item to update the character's statistics using an item-specific dynamic rule. One way to do this is to define an action function for the item instead of a dictionary of statistics [code=python] class Character(object): def __init__(self, name): self.name = name def useItem(self, item): … | |
Re: If you've learned about lists, a good way to store the numbers would be to append them to a list. But as gerard4143 said, you don't really need to store them. | |
Re: Your third parameter breaches the contract. It said that the third parameter must be the number of steps to be taken. It means that randint() must be called exactly this number of times. | |
Re: Many python functions which used to return lists now return iterables. You can convert it to a list with the list() constructor [code=python] list(map(lambda x,y:x+y, a,b)) [/code] This produces the same as [code=python] [x + y for x, y in zip(a, b)] [/code] The use of map() is deprecated in … | |
Re: Hm [icode]''.join(the_list).split()[/icode] | |
| |
Re: An improvement would be to use the argparse module to handle the command line. It would be especially useful to learn if you need to write a lot of scripts. | |
Re: You could matrix multiply on the right by matrix with 3 rows and 1 column [16*16, 16, 1]. | |
Re: It's not exactly a python question, although your problem could probably be programmed in python. Apparently you are trying to extract something from File.txt using the 2 columns ValueA and ValueB. Can you explain with more details the contents of File.txt and the expected output ? | |
Re: What's wrong in your result ? We don't have the input file, so it looks OK :) | |
Re: I found this [url]http://stackoverflow.com/questions/4799553/how-to-update-one-file-in-a-zip-archive[/url] . It's not python, but it could be a starting point. In windows you could try with 7zip. | |
Re: To clear the screen in a linux terminal, use this [code=python] print("\x1B[2J") [/code] | |
Re: You should be able to install xlwt with the pip installer. For this type [code=text] sudo pip install xlwt [/code] in a terminal (with a working internet connection). If you don't have pip, then you must install pip first. For this type [code=text] sudo easy_install pip [/code] If you don't … | |
Re: [QUOTE=shoemoodoshaloo;1777559]So basically "is" could be repaced with if id(a) == id(b)?[/QUOTE] Exactly, because id(a) is nothing but the C pointer to object a converted to integer (see the function builtin_id in [url]http://svn.python.org/view/python/trunk/Python/bltinmodule.c?revision=81029&view=markup[/url]) , and the [b]is[/b] operator is implemented by comparing the pointer values (in [url]http://svn.python.org/view/python/trunk/Python/ceval.c?view=markup[/url]) [code=c] static PyObject * … | |
Re: Indent the last 2 lines to put them in the while's body. | |
Re: Another solution is to catch the apps output in your python program. You may need to change the way your apps are launched. | |
Re: At first sight, I would say that it is not possible. If you look in the source code of the turtle module (in python 2.7), you'll see that the Turtle instances all use a [i]singleton[/i] object called turtle._screen. It means that multiple windows are probably not implemented. | |
Re: Here is a good starting point [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=2[/url] . | |
Re: It is possible that the (key, value) pairs where pickled one by one to the file using multiple calls to dump(). In this case, you can read them with multiple calls to load(). See this example [code=python] """ Example showing multiple calls of pickle.dump() to the same file. The objects … | |
Re: Hm, try this [code=python] outstr = "\n".join("\t".join(str(i*j) for j in range(1, 13)) for i in range(1, 13)) print outstr [/code] | |
Re: Also the function splitIt() from this post [url]http://www.daniweb.com/software-development/python/code/217111/969269#post969269[/url] works for lists. | |
Re: I think there are a lot of misconceptions about classes in your approach. First, a more accurate name for class Person would be MinimalDataThatThisProgramNeedsToStoreAboutAPerson. It means that the purpose of a class Person is not to mimic a person from the real world but to provide some space in memory … |
The End.