我想删除只是列名(x,y,z)只使用数据
In [68]: df
Out[68]:
x y z
0 1 0 1
1 2 0 0
2 2 1 1
3 2 0 1
4 2 1 0
Run Code Online (Sandbox Code Playgroud)
我想打印结果如下.
Out[68]:
0 1 0 1
1 2 0 0
2 2 1 1
3 2 0 1
4 2 1 0
Run Code Online (Sandbox Code Playgroud)
可能吗?我怎么能这样做?
Gunicorn有时使我的服务器崩溃,实际上以sys.exit(1)
!退出了Python解释器。为什么是这样?请注意,故障并不总是在同一点。在下面显示的两种情况下,gunicorn's之前有一个不同的最后一个代码行exit
。在这里运行的代码是openpyxl,这不会导致解释器关闭!
服务器内存不足吗?还有其他原因吗?
(这是Google容器引擎中Docker上Gunicorn上的Flask。)
情况1
File "/virtualenv_for_docker/lib/python3.7/site-packages/openpyxl/descriptors/base.py", line 166, in __set__
super(Bool, self).__set__(instance, value)
File "/virtualenv_for_docker/lib/python3.7/site-packages/gunicorn/workers/base.py", line 196, in handle_abort
sys.exit(1)
SystemExit: 1
Run Code Online (Sandbox Code Playgroud)
情况二
File "/virtualenv_for_docker/lib/python3.7/site-packages/openpyxl/descriptors/serialisable.py", line 164, in __eq__
def __eq__(self, other):
File "/virtualenv_for_docker/lib/python3.7/site-packages/gunicorn/workers/base.py", line 196, in handle_abort
sys.exit(1)
SystemExit: 1
Run Code Online (Sandbox Code Playgroud) I'm currently investigating the feasibility of adding HEVC support to videos but am hitting a problem with Safari. Here's the sample source:
<video autobuffer="true" autoloop="" loop="" controls="">
<source src="film_WebM.webm" type="video/webm">
<source src="film_HEVC.mp4" type="video/mp4">
<source src="film.mp4" type="video/mp4">
</video>
Run Code Online (Sandbox Code Playgroud)
Ideally a browser should read the sources and takt the first file that it thinks it can read and this should allow Firefox and Chromium to show the VP9 film, Safari the HEVC and Internet Explorer the H264. However, Safari doesn't play nicely …
我有一个应用程序,我需要能够尽快区分数字和bool.除先跑之外有哪些替代方案isinstance(value, bool)
?
编辑:感谢您的建议.实际上,我想要做的就是检查数字,这样可以减少我的支票(数字更为普遍),并改善我的负面因素.isinstance()
本身足够快.这x is True or x is False
很有趣.
从 Excel 工作表生成 pdf 时,我收到以下错误:
ws.ExportAsFixedFormat(0, save_as)
File "<COMObject <unknown>>", line 5, in ExportAsFixedFormat
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024809), None)
Run Code Online (Sandbox Code Playgroud)
下面是我的代码:
pythoncom.CoInitialize()
xlApp = client.Dispatch("Excel.Application")
logging.debug("Saving excel file {} to file {}".format(filename, save_as))
books = xlApp.Workbooks.Open(filename)
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, save_as)
books.Close(True)
xlApp.Quit()
Run Code Online (Sandbox Code Playgroud)
它在我安装了 Office 365 的笔记本电脑上运行,但在安装了 Microsoft Office 2007 的另一个系统上出现上述错误。
Python版本:python 2.7
openpyxl:2.4.5
pywin32:224
没有足够的文档。如果有人可以提供调试它并理解错误的指针,那将会有很大的帮助。
我对 Python Tornado 很陌生,一直在尝试启动一个新线程来运行一些 IO 阻塞代码,同时允许服务器继续处理新请求。我一直在做一些阅读,但似乎仍然无法弄清楚这两个功能之间的区别是什么?
例如调用这样的方法:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(1) as executor:
future = executor.submit(report.write_gresb_workbook)
print(future.result())
Run Code Online (Sandbox Code Playgroud)
相比:
from concurrent.futures import ThreadPoolExecutor
from tornado import ioloop
with ThreadPoolExecutor(1) as executor:
my_success = await ioloop.IOLoop.current().run_in_executor(executor, report.write_gresb_workbook)
print(my_success)
Run Code Online (Sandbox Code Playgroud)
write_gresb_workbook 从对象报告中获取一些信息并将其写入 Excel 电子表格(但是我使用 openpyxl 需要大约 20 秒才能加载适当格式的工作簿,另外大约需要 20 秒才能保存它,这会阻止服务器处理新请求!)
该函数简单地返回 True 或 False(这是什么my_success
),因为报告对象具有附加到它的输出文件的路径。
我还没有完全使用这些方法中的任何一种,所以它们可能不正确,但只是在寻找一些背景信息。
干杯!
我想将行索引和列索引转换为Excel字母数字单元格引用,例如“ A1”。我正在使用python和openpyxl,我怀疑该程序包中的某个地方可以执行此操作,但是经过一些搜索后我什么都没找到。
我写了下面的代码,它可以工作,但是我宁愿使用openpyxl软件包中的一部分(如果有的话)。
def xlref(row,column):
"""
xlref - Simple conversion of row, column to an excel string format
>>> xlref(0,0)
'A1'
>>> xlref(0,26)
'AA1'
"""
def columns(column):
from string import uppercase
if column > 26**3:
raise Exception("xlref only supports columns < 26^3")
c2chars = [''] + list(uppercase)
c2,c1 = divmod(column,26)
c3,c2 = divmod(c2,26)
return "%s%s%s" % (c2chars[c3],c2chars[c2],uppercase[c1])
return "%s%d" % (columns(column),row+1)
Run Code Online (Sandbox Code Playgroud)
有人知道更好的方法吗?
我需要在 Excel 工作表中搜索包含某种模式的单元格。这需要的时间比我能处理的要多。我能写的最优化的代码如下。由于数据模式通常是一行一行的,所以我使用 iter_rows(row_offset=x)。不幸的是,下面的代码在每个 for 循环中发现给定模式的次数越来越多(从几毫秒开始到几乎一分钟)。我究竟做错了什么?
import openpyxl
import datetime
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.title = "test_sheet"
print("Generating quite big excel file")
for i in range(1,10000):
for j in range(1,20):
ws.cell(row = i, column = j).value = "Cell[{},{}]".format(i,j)
print("Saving test excel file")
wb.save('test.xlsx')
def FindXlCell(search_str, last_r):
t = datetime.datetime.utcnow()
for row in ws.iter_rows(row_offset=last_r):
for cell in row:
if (search_str == cell.value):
print(search_str, last_r, cell.row, datetime.datetime.utcnow() - t)
last_r = cell.row
return last_r
print("record not …
Run Code Online (Sandbox Code Playgroud) 我试图访问给定的文件夹,然后访问其所有子文件夹(20+),然后访问xlsx
每个子文件夹内的唯一文件进行一些计算.
我的代码使用load_workbook
从openpyxl
.我需要一个for循环来读取同一文件夹中的现有文件,但这些文件将其名称从子文件夹更改为子文件夹.为了解决将文件load_workbook
的精确名称xlsx
作为输入的事实,我选择了这个解决方案:
filename=os.path.basename(file)
wb=load_workbook(filename)
Run Code Online (Sandbox Code Playgroud)
但它引发了这个错误:AttributeError: 'list' object has no attribute 'endswith'
.如何解决我的解决方法?
我的完整脚本:
import os
from openpyxl import load_workbook
directoryPath=r'C:\Users\MyName\Desktop\MyFolder'
os.chdir(directoryPath)
folder_list=os.listdir(directoryPath)
for folders, sub_folders, file in os.walk(directoryPath):
for name in file:
if file.endswith(".xlsx"):
filename=os.path.basename(file) #This is supposed to dump the name of the current file to a variable to be used by load_workbook
wb=load_workbook(filename)
cell_range = wb['A1':'A10'] #Accessing some cells
#some calculations
Run Code Online (Sandbox Code Playgroud)