Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

7
从subprocess.communicate()读取流输入
我正在使用Pythonsubprocess.communicate()从运行约一分钟的进程中读取stdout。 如何stdout以流方式打印出该流程的每一行,以便可以看到生成的输出,但是仍然阻止该流程终止,然后再继续? subprocess.communicate() 似乎一次给出所有输出。

7
如何在CSV文件中写入UTF-8
我正在尝试从PyQt4创建csv格式的文本文件QTableWidget。我想用UTF-8编码写文本,因为它包含特殊字符。我使用以下代码: import codecs ... myfile = codecs.open(filename, 'w','utf-8') ... f = result.table.item(i,c).text() myfile.write(f+";") 它一直工作到单元格包含特殊字符为止。我也尝试过 myfile = open(filename, 'w') ... f = unicode(result.table.item(i,c).text(), "utf-8") 但是,当出现特殊字符时,它也会停止。我不知道我在做什么错。
83 python  csv  encoding  utf-8 

2
Python和带Unicode的正则表达式
我需要从字符串“ بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ”中删除一些Unicode符号 我知道他们肯定在这里。我试过了: re.sub('([\u064B-\u0652\u06D4\u0670\u0674\u06D5-\u06ED]+)', '', 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ') 但这不起作用。字符串保持不变。我究竟做错了什么?


8
从virtualenv调用IPython
我知道IPython不支持virtualenv,对此最合乎逻辑的解决方案是使用以下命令分别在每个virtualenv中安装ipython pip install ipython 到现在为止还挺好。我注意到的一件事是,如果在将IPython$> ipython安装在此virtualenv下之前从virtualenv中调用IPython的系统范围的副本,则后续$> ipython命令将继续显示系统范围的ipython副本。 另一方面,如果在virtualenv下安装ipython之前未调用它,$> ipython则会弹出新安装的副本。 这有什么解释? 这也让我怀疑这种行为是否意味着我应该期待一些麻烦?

11
Python继承的意义是什么?
假设您有以下情况 #include <iostream> class Animal { public: virtual void speak() = 0; }; class Dog : public Animal { void speak() { std::cout << "woff!" <<std::endl; } }; class Cat : public Animal { void speak() { std::cout << "meow!" <<std::endl; } }; void makeSpeak(Animal &a) { a.speak(); } int …

5
如何*实际*读取TensorFlow中的CSV数据?
我在TensorFlow领域相对较新,对您如何将CSV数据实际读入TensorFlow中可用的示例/标签张量的方式感到困惑。TensorFlow教程中有关读取CSV数据的示例相当分散,仅使您成为能够训练CSV数据的一部分。 这是我根据CSV教程整理而成的代码: from __future__ import print_function import tensorflow as tf def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 filename = "csv_test_data.csv" # setup text reader file_length = file_len(filename) filename_queue = tf.train.string_input_producer([filename]) reader = tf.TextLineReader(skip_header_lines=1) _, csv_row = reader.read(filename_queue) # setup CSV decoding …
83 python  csv  tensorflow 

12
如何将浮点数四舍五入到小数点后一位?
假设我有8.8333333333333339,我想将其转换为8.84。如何在Python中完成此操作? round(8.8333333333333339, 2)给与8.83不8.84。我是Python或一般编程的新手。 我不想将其打印为字符串,结果将被进一步使用。有关此问题的更多信息,请查看Tim Wilson的Python编程技巧:贷款和付款计算器。


8
不带括号的Python 3打印
在print曾经是在Python 2中的语句,如今却成了一个需要括号在Python 3的功能。 无论如何,在Python 3中是否有抑制这些括号的内容?也许通过重新定义打印功能? 所以,代替 print ("Hello stack over flowers") 我可以输入: print "Hello stack over flowers"
83 python  printing 

9
如何检查Python中是否存在方法?
在函数中__getattr__(),如果未找到引用的变量,则会给出错误。我如何检查变量或方法是否作为对象的一部分存在? import string import logging class Dynamo: def __init__(self,x): print "In Init def" self.x=x def __repr__(self): print self.x def __str__(self): print self.x def __int__(self): print "In Init def" def __getattr__(self, key): print "In getattr" if key == 'color': return 'PapayaWhip' else: raise AttributeError dyn = Dynamo('1') print dyn.color dyn.color = …
83 python  methods 

3
Django的不存在
我在尝试计算“ DoesNotExist错误”时遇到问题,我试图找到管理无答案结果的正确方法,但是在“ DoesNotExist”或“对象没有属性DoestNotExists”上仍然存在问题 from django.http import HttpResponse from django.contrib.sites.models import Site from django.utils import simplejson from vehicles.models import * from gpstracking.models import * def request_statuses(request): data = [] vehicles = Vehicle.objects.filter() Vehicle.vehicledevice_ for vehicle in vehicles: try: vehicledevice = vehicle.vehicledevice_set.get(is_joined__exact = True) imei = vehicledevice.device.imei try: lastposition = vehicledevice.device.devicetrack_set.latest('date_time_process') altitude …

3
如何使用Scipy.signal.butter实现带通Butterworth滤波器
更新: 我发现了基于此问题的Scipy食谱!因此,对于感兴趣的任何人,请直接转到:目录»信号处理»Butterworth Bandpass 我很难实现最初看起来像是为一维numpy数组(时间序列)实现Butterworth带通滤波器的简单任务。 我必须包括的参数是sample_rate,HERTZ的截止频率以及可能的阶数(其他参数(例如衰减,固有频率等)对我来说比较晦涩,因此任何“默认”值都可以)。 我现在所拥有的就是这个,它似乎可以用作高通滤波​​器,但是我不确定自己是否做对了: def butter_highpass(interval, sampling_rate, cutoff, order=5): nyq = sampling_rate * 0.5 stopfreq = float(cutoff) cornerfreq = 0.4 * stopfreq # (?) ws = cornerfreq/nyq wp = stopfreq/nyq # for bandpass: # wp = [0.2, 0.5], ws = [0.1, 0.6] N, wn = scipy.signal.buttord(wp, ws, 3, …

3
合并两个熊猫数据框(在同一列上连接)
我有2个数据框: restaurant_ids_dataframe Data columns (total 13 columns): business_id 4503 non-null values categories 4503 non-null values city 4503 non-null values full_address 4503 non-null values latitude 4503 non-null values longitude 4503 non-null values name 4503 non-null values neighborhoods 4503 non-null values open 4503 non-null values review_count 4503 non-null values stars 4503 non-null …

11
如何在Python中绘制ROC曲线
我正在尝试绘制ROC曲线,以评估使用Logistic回归软件包在Python中开发的预测模型的准确性。我已经计算了真实的阳性率和错误的阳性率。但是,我无法弄清楚如何使用matplotlib和计算AUC值正确绘制这些图。我该怎么办?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.