小编dot*_*.Py的帖子

检查是否存在大文件而不下载

不知道这是否可行,但是我想检查HTTP请求到大文件的状态代码,而无需下载它。我只想检查服务器上是否存在。

是否可以使用Python做到这一点requests?我已经知道如何检查状态码,但是只有在下载文件后才能执行此操作。

我想我要问的是,您是否可以发出GET请求并在收到响应标头后立即将其停止?

http-headers web-scraping python-3.x python-requests

7
推荐指数
1
解决办法
1795
查看次数

如何在 Python 中使用文本文件作为数据库?

我想知道将多个数据作为数据库逻辑插入文本文件的有效方法是什么?

例如我有一组汽车,这些汽车有它们的id, name, 和color变量。

id | name | color
1  | car1 | green
2  | car2 | red
3  | car3 | blue
Run Code Online (Sandbox Code Playgroud)

我想使用 python 将上面的数据插入到文本文件中。

后来我想搜索一辆车,例如根据它的颜色。

我应该遵循哪种方法?一个带有解释的例子将不胜感激!

python database python-2.7

7
推荐指数
1
解决办法
1万
查看次数

在 chrome 中自动打印/保存网页为 pdf - python 3.6

我正在尝试创建一个脚本,通过 Chrome 的打印功能自动保存只读 pdf,以将其另存为同一文件夹中的另一个 pdf。这将删除“只读”功能。但是,在运行脚本时,我不确定在哪里可以指定我自己的特定目标文件夹,脚本将其直接保存在下载文件夹中。

以下代码的完整道具/sf/users/100283011/

任何帮助将不胜感激。

import json
from selenium import webdriver
downloadPath = r'mypath\downloadPdf\\'
appState = {
"recentDestinations": [
    {
        "id": "Save as PDF",
        "origin": "local"
    }
],
"selectedDestinationId": "Save as PDF",
"version": 2
}

profile = {'printing.print_preview_sticky_settings.appState':json.dumps(appState)}

chrome_options = webdriver.ChromeOptions() 
chrome_options.add_experimental_option('prefs', profile) 
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(chrome_options=chrome_options) 
pdfPath = r'mypath\protected.pdf' 
driver.get(pdfPath) 
driver.execute_script('window.print();')
Run Code Online (Sandbox Code Playgroud)

python selenium pdf-generation google-chrome

6
推荐指数
1
解决办法
6573
查看次数

当时间戳未被归类为索引时,如何按时间戳对数据帧进行切片?

如何通过使用时间戳来拆分我的 Pandas 数据帧?

我打电话时得到以下价格df30m

               Timestamp    Open    High     Low   Close     Volume
0    2016-05-01 19:30:00  449.80  450.13  449.80  449.90    74.1760
1    2016-05-01 20:00:00  449.90  450.27  449.90  450.07    63.5840
2    2016-05-01 20:30:00  450.12  451.00  450.02  450.51    64.1080
3    2016-05-01 21:00:00  450.51  452.05  450.50  451.22    75.7390
4    2016-05-01 21:30:00  451.21  451.64  450.81  450.87    71.1190
5    2016-05-01 22:00:00  450.87  452.05  450.87  451.07    73.8430
6    2016-05-01 22:30:00  451.09  451.70  450.91  450.91    68.1490
7    2016-05-01 23:00:00  450.91  450.98  449.97  450.61    84.5430
8    2016-05-01 23:30:00  450.61 …
Run Code Online (Sandbox Code Playgroud)

python timestamp split dataframe pandas

5
推荐指数
1
解决办法
8426
查看次数

可变移动平均线

我有一个看起来像这样的DataFrame:

    a    b             
1   0.9  0.796522123    
2   0.8  0.701075019    
3   0.6  0.777130253    
4   0.5  0.209912906    
5   0.75 0.920537662    
6   1    0.955212665    
7   3.5  0.227221963    
8   2    0.336632891    
9   1.25 0.563511758    
10  1    0.832624112    
Run Code Online (Sandbox Code Playgroud)

我想创建一个最大周期为3的移动平均线,每次观察都是df['a']*df['b].

如果df['a'].rolling(window=3).sum() <= 3,那么MA将是:

df['MA'] = (df['a']*df['b']).rolling(window=3).mean().

但是,例如,如果df['a'].rolling(window=3).sum() > 3情况如此df[8:10],那么我希望移动平均线为:

((1*0.83)+(1.25*0.832624112)+(0.75*0.336632891))/3.

我一直在玩弄创建一个函数,然后应用它,如:

def MA(a, b, period):
    total = 0
    sum_a = 0
    for i in (b):
        if sum_a < period:
            sum_a += a
            total += (a*b)
        else: …
Run Code Online (Sandbox Code Playgroud)

python finance quantitative-finance python-3.x pandas

5
推荐指数
1
解决办法
476
查看次数

仅在单个NaN时插值

大熊猫有没有办法只插入一个缺失的数据点?也就是说,如果有2个以上连续的NaN,我想让它们独自一人.

所以,举个例子:

s = pd.Series([1, None, 2, 3, None, None, 4.5])
d.interpolate(limit=1)
Run Code Online (Sandbox Code Playgroud)

给我:

[ 1.0, 1.5, 2.0, 3.0, 3.5, NaN, 4.5 ]
Run Code Online (Sandbox Code Playgroud)

但是我想得到

[ 1.0, 1.5, 2.0, 3.0, NaN, NaN, 4.5 ]
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,我有一个索引列表,其中只有一个缺失值.

python numpy scipy pandas

4
推荐指数
1
解决办法
311
查看次数

python Hackerrank中的EOF错误

试图解决问题,但是Hackerrank的编译器在解析时不断抛出错误EOFError:不知道哪里出错了。

#!usr/bin/python

b=[]
b=raw_input().split()
c=[]
d=[]
a=raw_input()
c=a.split()
f=b[1]
l=int(b[1])
if(len(c)==int(b[0])):          
    for i in range(l,len(c)):
        d.append(c[i])
        #print c[i]
    for i in range(int(f)):
        d.append(c[i])
        #print c[i]
for j in range(len(d)):
    print d[j],
Run Code Online (Sandbox Code Playgroud)

我也尝试尝试抓住解决它,但随后没有输入。

try:
    a=input()
    c=a.split()
except(EOFError):
    a=""
Run Code Online (Sandbox Code Playgroud)

输入格式是2个间隔开的整数,然后是数组

追溯错误是:

Traceback (most recent call last):
  File "solution.py", line 4, in <module>
    b=raw_input().split()
EOFError: EOF when reading a line
Run Code Online (Sandbox Code Playgroud)

python eoferror

4
推荐指数
2
解决办法
9479
查看次数

删除字符串中的所有标点符号,数字之间的标点符号除外

我有一篇包含单词和数字的文本。我将给出一个有代表性的文本示例:

string = "This is a 1example of the text. But, it only is 2.5 percent of all data"
Run Code Online (Sandbox Code Playgroud)

我想将其转换为类似的东西:

"This is a  1 example of the text But it only is  2.5  percent of all data"
Run Code Online (Sandbox Code Playgroud)

因此,删除标点符号(可以是. ,或 中的任何其他标点符号string.punctuation),并在连接时在数字和单词之间放置空格。但在我的示例中保持浮点数为 2.5。

我使用了以下代码:

item = "This is a 1example of the text. But, it only is 2.5 percent of all data"
item = ' '.join(re.sub( r"([A-Z])", r" \1", item).split())
# This a start but not …
Run Code Online (Sandbox Code Playgroud)

python regex string text mining

3
推荐指数
1
解决办法
7746
查看次数

Python 下载嵌入在页面中的 PDF

我有这个链接:

我想下载嵌入的 PDF。

我已经尝试了urllib和的正常方法,request但它们不起作用。

import urllib2

url = "http://www.equibase.com/premium/chartEmb.cfm?track=ALB&raceDate=06/17/2002&cy=USA&rn=1"
response = urllib2.urlopen(url)
file = open("document.pdf", 'wb')
file.write(response.read())
file.close()
Run Code Online (Sandbox Code Playgroud)

此外,我也试图找到pdf的原始链接,但也没有用。

内部链接:

python pdf web-scraping

3
推荐指数
1
解决办法
6999
查看次数

或列表理解中的条件

有没有办法以更简洁的方式完成下面的内容?如果我有很多值,那么我的脚本将变得非常难以理解.

现行守则

import xlrd  
filename = r'H:\Book1.xls'
wb = xlrd.open_workbook(filename)
sh1 = wb.sheet_by_index(0)
data = [sh1.row_values(row) for row in range(sh1.nrows) if 1 in sh1.row_values(row) or 9 in sh1.row_values(row) ]
print(data)
Run Code Online (Sandbox Code Playgroud)

代码结果

[[1.0, 2.0, 3.0, '', 4.0, '', 6.0], ['', '', '', '', 9.0, '', '']]
Run Code Online (Sandbox Code Playgroud)

期望的语法

data = [sh1.row_values(row) for row in range(sh1.nrows) if 1,9 in sh1.row_values(row)]
Run Code Online (Sandbox Code Playgroud)

是否可以传入列表或包含1,9等的对象?

python list-comprehension conditional-statements

2
推荐指数
1
解决办法
81
查看次数