python-2.x

One Python script giving “user input” to another python script

一笑奈何 提交于 2021-02-18 07:41:05
问题 this is my first question, I hope I'm doing this right. let's say I have these this file: "simple.py": a=raw_input("your name?") print "Hello",a but with a different script, I want to execute "simple.py" many time and giving the input automatically, that would work like: "everyone.py" run simple.py input=Alice run simple.py input=Bob ... to get "Hello Alice" "Hello Bob" ... I know it's possible to make "everyone.py" run "simple.py" by using os.system, but is there any practical way to do

Creating tuples using a variable number of for loops

半城伤御伤魂 提交于 2021-02-10 12:15:41
问题 Given n and k , I need to create all tuples of length k whose entries are from range(n) (0 to n-1) such that the tuple's entries are in dictionary order and there are parentheses in a particular format. Specifically, the tuple has parentheses around each pair, from inside out. For example, if n=3 and k=4 , then I would like the output to include something like (((0,0),1),2) , but not something like (((0,0),2),1) . The code below works for this specific instance. The issue is that I don't know

raw_input prints prompt after asking for input

こ雲淡風輕ζ 提交于 2021-02-10 03:55:30
问题 When I use raw_input, the prompt only shows after the user gives input. Like this: number = raw_input("Enter a number:") but when I run this, nothing happens, the I type a number, the it shows the prompt: 123 Enter a number: (123 used to be blank until I typed a number and hit enter) I just want the prompt to display before the user input. If anybody knows how to fix this please help. Thanks. 回答1: You might be in an environment where your standard output is buffered and won't flush until

Why does this circular import fail in Python 2 but not in Python 3?

别等时光非礼了梦想. 提交于 2021-02-08 08:41:22
问题 Assume the following package structure with code inline: main.py from a.b import c a/__init__.py a/b/__init__.py a/b/c.py from a.b import d a/b/d.py from a.b import c python2 main.py gives me an import error: Traceback (most recent call last): File "main.py", line 1, in <module> from a.b import c File "/home/runner/a/b/c.py", line 1, in <module> from a.b import d File "/home/runner/a/b/d.py", line 1, in <module> from a.b import c ImportError: cannot import name c However python3 main.py works

Checking user input using isnan function of NumPy

安稳与你 提交于 2021-02-07 14:20:21
问题 I'm trying to use NumPy to check if user input is numerical. I've tried using: import numpy as np a = input("\n\nInsert A: ") if np.isnan(a): print 'Not a number...' else: print "Yep,that's a number" On its own t works fine, however when I embed it into a function such as in this case: import numpy as np def test_this(a): if np.isnan(a): print '\n\nThis is not an accepted type of input for A\n\n' raise ValueError else: print "Yep,that's a number" a = input("\n\nInsert A: ") test_this(a) Then

Write to terminal in Tkinter GUI

别说谁变了你拦得住时间么 提交于 2021-02-07 10:14:55
问题 I am trying to show the changes in the command line in real time in my Tkinter GUI, I managed to create the GUI and integrate the terminal into it, but I cant bind the buttons with the terminal, my code is : import Tkinter from Tkinter import * import subprocess import os from os import system as cmd WINDOW_SIZE = "600x400" top = Tkinter.Tk() top.geometry(WINDOW_SIZE) def helloCallBack(): print "Below is the output from the shell script in terminal" subprocess.call('perl /projects/tfs/users/

Write to terminal in Tkinter GUI

[亡魂溺海] 提交于 2021-02-07 10:11:02
问题 I am trying to show the changes in the command line in real time in my Tkinter GUI, I managed to create the GUI and integrate the terminal into it, but I cant bind the buttons with the terminal, my code is : import Tkinter from Tkinter import * import subprocess import os from os import system as cmd WINDOW_SIZE = "600x400" top = Tkinter.Tk() top.geometry(WINDOW_SIZE) def helloCallBack(): print "Below is the output from the shell script in terminal" subprocess.call('perl /projects/tfs/users/

comparing two numpy 2D arrays for similarity

白昼怎懂夜的黑 提交于 2021-01-29 08:48:29
问题 I have 2D numpy array1 that contains only 0 and 255 values ([[255, 0, 255, 0, 0], [ 0, 255, 0, 0, 0], [ 0, 0, 255, 0, 255], [ 0, 255, 255, 255, 255], [255, 0, 255, 0, 255]]) and an array2 that is identical in size and shape as array1 and also contains only 0 and 255 values ([[255, 0, 255, 0, 255], [ 0, 255, 0, 0, 0], [255, 0, 0, 0, 255], [ 0, 0, 255, 255, 255], [255, 0, 255, 0, 0]]) How can I compare array1 to array2 to determine a similarity percentage? 回答1: As you only have two possible

Selecting multiple specific columns in excel and exporting to CSV using Python

心不动则不痛 提交于 2021-01-29 07:52:55
问题 I'm trying to extract 3 columns in a large excel spreadsheet and export it to CSV format. I have XLRD but have found that there are no column specific tools I can use (Or maybe I just overlooked it). An example excel spreadsheet I have looks like the following Column1 Column2 Column3 Column4 Column5 Column6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 I'm looking to get just Column2, Column5, and Column6 into a CSV. The other columns are for

psutil.AccessDenied when using StanfordCoreNLP in Pycharm? [duplicate]

南楼画角 提交于 2021-01-29 05:20:27
问题 This question already has answers here : psutil.AccessDenied Error while trying to load StanfordCoreNLP (3 answers) Closed 6 months ago . # coding=utf-8 from stanfordcorenlp import StanfordCoreNLP nlp = StanfordCoreNLP(r'/Users/silas/stanford-corenlp/', lang='zh') sentence = '清华大学位于北京。' print nlp.word_tokenize(sentence) print nlp.pos_tag(sentence) print nlp.ner(sentence) print nlp.parse(sentence) print nlp.dependency_parse(sentence) nlp.close() I'm using Mac. Java, NLKT, and Stanforcorenlp