天道酬勤,学无止境

python-2.7

Python csv skip first two empty rows

Before anyone marks this as duplicate, I have tried everything from isspace, startswith, itertools filterfunction, readlines()[2:]. I have a Python script that searches hundreds of CSV files and prints the row with the matching string (in this case a unique ID) in the eighth column from the left. import csv import glob csvfiles = glob.glob('20??-??-??.csv') for filename in csvfiles: reader = csv.reader(open(csvfiles)) for row in reader: col8 = str(row[8]) if col8 == '36862210': print row The code works with test .csv files. However, the real .csv files I'm working with all have blank first two

2021-10-27 03:49:24    分类:问答    python   python-2.7   csv

Python: UnicodeDecodeError using pandas.read_excel

import pandas as pd msf=pd.read_excel('‪C:\Users\pauldufosse\Documents\TESTDANIEL.xlsx', has_index_names=True) I'm having difficulties importing this xlsx document. I will give you the full traceback as you might help me to find WHERE the wrong byte is. I'm french so there is the character 'à' but I think this one is taken in charge by ascii/utf-8 am i right? runfile('C:/Users/pauldufosse/Documents/MSF.py', wdir='C:/Users/pauldufosse/Documents') Traceback (most recent call last): File "C:\Users\pauldufosse\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3052, in run_code

2021-10-27 03:33:32    分类:问答    python   python-2.7   pandas   ascii   xlsx

How to find all overlaps of two strings in python

I have two strings, for example 'bbb', and 'bbab', and I want to find all of the overlaps between them (which in this case would be 'bbbbab', 'bbbab', and 'bbabbb'). Is there a python program in the documentation that does this?

2021-10-27 03:27:36    分类:问答    python-2.7

Pycharms debugger running on a different Python interpreter in subprocess

I have a PyCharms python project configured to run on a virtual env (Python 2.7.9). Off late , trying to debug the project on PyCharms used to throw import errors for many modules. The code works just fine when invoked from a command prompt running same virtual env or while "Run"ing the project from PyCharms. Upon inspection I found that for some reason PyCharms is invoking Python 2.7.10 and probably the third party dependencies are not installed in there !. My Question is, Is there a reason why PyCharms is moving away from the virtual env when invoking subprocess and what would be the best

2021-10-27 02:52:51    分类:问答    python   python-2.7   debugging   pycharm

Python class inherited singleton inits instance on every call

I'm trying to implement class inherited singleton as described here (Method 2). Going over the question and the extensive chosen answer I tried to implement the following: class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not isinstance(cls._instance, cls): cls._instance = object.__new__(cls, *args, **kwargs) cls._instance._initialized = False return cls._instance class A(Singleton): def __init__(self): print "Init is called" class B(Singleton): def __init__(self): print "Init is called" As you may guess, whenever I create Class A I get the same object, but __init

2021-10-27 02:49:00    分类:问答    python   python-2.7   inheritance   singleton   metaclass

pywin32 and pyttsx error, trouble combining the two

i have pywin32 in my site packages and my pyttsx is in a separate folder. Is this the reason why i am getting the following error? import win32api, sys, os ImportError: DLL load failed: The specified module could not be found The code is as follows, import pyttsx def onStart(name): print 'starting', name def onWord(name, location, length): print 'word', name, location, length def onEnd(name, completed): print 'finishing', name, completed engine = pyttsx.init() engine.connect('started-utterance', onStart) engine.connect('started-word', onWord) engine.connect('finished-utterance', onEnd) engine

2021-10-27 01:11:20    分类:问答    python-2.7   pywin32   pyttsx

如何以标准方式将新数组添加到现有 npz 文件?(How to add a new array to an existing npz file in a standard way?)

问题 我有一个将数组写入压缩的*.npz文件的函数: def save_a(file): np.savez_compressed(file, a=[[1, 2, 3]]) 我想创建一个调用函数save_a()的函数,然后将另一个数组添加到文件中(例如使用元数据): def save_a_b(file): save_a(file) np.savez_compressed(file, b=[len(save_a.__name__)]) 可悲的是,当我尝试使用函数save_a_b() ,我覆盖了以前的数组: buffer = io.BytesIO() save_a_b(buffer) buffer.seek(0) with np.load(buffer) as fh: for name in fh: print(name, fh[name]) 我需要在不使用的情况下实现它: 私有方法, 破解.npz格式,除非保证其规范在未来向后兼容。 我更喜欢适用于文件对象和str路径的解决方案。

2021-10-26 23:43:06    分类:技术分享    python   python-3.x   python-2.7   numpy   numpy-ndarray

获取一列列表的频率表(Get a frequency table for a column of lists)

问题 假设我有 DataFrame,其中有一列列表。 df = pd.DataFrame({'A': [['a', 'b', 'c'], ['b'], ['c'], ['a', 'b']]}) 与输出 Index A 0 ['a', 'b', 'c'] 1 ['b'] 2 ['c'] 3 ['a', 'b'] 如何获取列中列表出​​现频率的频率表? 理想的输出看起来像 A Count ['a', 'b', 'c'] 1 ['b'] 1 ['c'] 1 ['a', 'b'] 1 尝试这样的事情...... df.A.value_counts() 导致错误 TypeError: unhashable type: 'list' 回答1 map到元组,列表不可散列,因为错误提示: df.A.map(tuple).value_counts().rename_axis('A').reset_index(name='Count') A Count 0 (a, b, c) 1 1 (a, b) 1 2 (b,) 1 3 (c,) 1 回答2 您还可以使用apply转换为元组: In [423]: df.A.apply(tuple).value_counts().rename_axis('A').reset_index(name='Count') Out[423]: A Count 0 (a, b)

2021-10-26 23:09:17    分类:技术分享    python   pandas   python-2.7   dataframe

Msys2 升级打破了 python2-pyqt5(Msys2 upgrade breaks python2-pyqt5)

问题 我在 Win 10 下使用 PortableApps 的 msys2。 当我尝试使用pacman -Syuu进行全面升级时,我得到 :: Synchronizing package databases... mingw32 456.6 KiB 773 KiB/s 00:01 [###########################################################] 100% mingw32.sig 119.0 B 0.00 B/s 00:00 [###########################################################] 100% mingw64 459.4 KiB 792 KiB/s 00:01 [###########################################################] 100% mingw64.sig 119.0 B 0.00 B/s 00:00 [###########################################################] 100% msys is up to date :: Starting core system upgrade... there is nothing to do

2021-10-26 22:10:20    分类:技术分享    python   python-2.7   pyqt5   upgrade   msys2

Pandas left merging 'Date' keys with different date formats (Not Timestamps)

Hello Stack Overflow community, I am having an issue where Pandas is not understanding my merge conditions. It works with the other 'keys', but breaks as soon as I include the "Date" column as a key. The "Date" columns are string objects in both dataframes (not timestamps). In other words, I want all 4 'keys' to be identical before "left merging" the columns from df2 to df without losing any data in df. Also, when I open the csv files in Excel, the dates format look exactly the same (ex: 5/10/2015). But, Pandas reads the date column in "csv_file1", [df], as "5-6-2015" : In [1]: df['Date'] Out

2021-10-26 21:19:56    分类:问答    python-2.7   pandas   merge   key   dataframe