社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Rakesh

Rakesh 最近回复了
5 年前
回复了 Rakesh 创建的主题 » 在python中获取所有%(name)的占位符

使用正则表达式。

前任:

import re

s = "This is a %(name)s example string %(foo)s I would like %(bar)s to extract all the placeholders from %(place)s"
print(re.findall(r"%\((.*?)\)", s))
# --> ['name', 'foo', 'bar', 'place']
5 年前
回复了 Rakesh 创建的主题 » Python列表操作

使用 collections.defaultdict

from collections import defaultdict


main_list = [["user1@test.com", "Administration", "100"],
             ["user2@test.com", "Testing", "30"],
             ["user2@test.com", "Development", "45"],
             ["user2@test.com", "Development", "90"],
             ["user2@test.com", "Development", "35"],
             ["user3@test.com", "Development", "400"],
             ["user3@test.com", "Administration", "95"],
             ["user3@test.com", "Testing", "200"]]
result = defaultdict(int)
for k, v, n in main_list:
    result[(k, v)] += int(n)
result = [list(k) + [v] for k, v in result.items()]
print(result)

输出:

[['user1@test.com', 'Administration', 100],
 ['user2@test.com', 'Testing', 30],
 ['user2@test.com', 'Development', 170],
 ['user3@test.com', 'Development', 400],
 ['user3@test.com', 'Administration', 95],
 ['user3@test.com', 'Testing', 200]]
5 年前
回复了 Rakesh 创建的主题 » 在python中使用writerow附加到我的csv

使用 next(input_reader) 获取标题,然后附加新列名并将其写回csv。

with open('simpleexcel.csv', 'r') as f_input:  # Declared variable f_input to open and read the input file
    input_reader = csv.reader(f_input)  # this will iterate ober lines from input file

    with open('Outputfile.csv', "w", newline='') as f_output:  # opens a file for qwriting in this case outputfile
        line_writer = csv.writer(f_output) #If csvfile is a file object, it should be opened with newline=''
        line_writer.writerow(next(input_reader) + ["Age"]))  #Write Header
        for line in input_reader: #prints every row
            line_writer.writerow(line+['14'])
6 年前
回复了 Rakesh 创建的主题 » 如何在Python中减去两个长列表中特定切片的对应元素?

你可以用 enumerate

前任:

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
y = [None,None,None,None,None,10,20,30,40,50,60,70,80,90,100]

result = []
for i, v in enumerate(x[5:], 5):
    result.append(v - y[i])
print(result)

[-4, -13, -22, -31, -40, -49, -58, -67, -76, -85]
6 年前
回复了 Rakesh 创建的主题 » 如何使while true循环在python中运行?

你可以用 range

前任:

data=[]

for _ in range(2):
    name = raw_input("Please enter your name: ")
    age = int(raw_input("Please enter your age: "))
    height = int(raw_input("Please enter your height: "))
    data.append((name, age, height))
print(data)

或: 使用while循环。

data=[]

while True:
    name = raw_input("Please enter your name: ")
    age = int(raw_input("Please enter your age: "))
    height = int(raw_input("Please enter your height: "))
    data.append((name, age, height))

    if len(data) == 2:
        break

print(data)
5 年前
回复了 Rakesh 创建的主题 » 在python中,只在双引号后拆分字符串

您也可以使用 csv 模块。

前任:

import csv

s = '"BLAX", "BLAY", "BLAZ, BLUBB", "BLAP"' 
r = csv.reader(s, delimiter = ',', quotechar='"')
res = [j for i in r for j in i if j.strip()] 
print(res)  

输出:

['BLAX', 'BLAY', 'BLAZ, BLUBB', 'BLAP']
6 年前
回复了 Rakesh 创建的主题 » 列表中实例数相同的python max

使用 collections.Counter

前任:

from collections import Counter

example = [('dog'),('dog'),('cat'),('cat'),('fish'),('frog'),('frog')]
c = Counter(example)
print(c.most_common(3))

输出:

[('dog', 2), ('frog', 2), ('cat', 2)]
6 年前
回复了 Rakesh 创建的主题 » python:beautifulsoup列表中<li>的内容

您可以使用嵌套列表理解

前任:

from bs4 import BeautifulSoup

html = """<ul>
<li>
  <div>Content1</div>
</li>
<li>
  <div>Content2</div>
  <div>Content3</div>
  <div>Content4</div>
</li>
<li>
  <div>Content5</div>
  <div>Content6</div>
</li>
</ul>"""

soup = BeautifulSoup(html, "html.parser")
print([[j.get_text(strip=True) for j in i.find_all("div")] for i in soup.find_all("li")])

输出:

[['Content1'], ['Content2', 'Content3', 'Content4'], ['Content5', 'Content6']]
6 年前
回复了 Rakesh 创建的主题 » python-从url参数读取特殊字符

你可能需要使用 %2B

前任:

http://exaple.com/api/v1/get_example/?fruits_name=[apple%20%2B%20banana]

Reference

6 年前
回复了 Rakesh 创建的主题 » 正则表达式python数据提取

这是一种使用简单迭代的方法。

前任:

s = """TASK000123-Tomcat server hosted on tbu.test1 is down-P1 --In Progress
TASK000123-Tomcat server hosted on tbu.test1 is down-P1 --Completed"""

result = [["TaskID","Priority","Status"]]

for i in s.splitlines():
    val = i.split("-")                          #Split by '-'
    result.append([val[0], val[2], val[-1]])
print(result)

产量:

[['TaskID', 'Priority', 'Status'],
 ['TASK000123', 'P1 ', 'In Progress'],
 ['TASK000123', 'P1 ', 'Completed']]
6 年前
回复了 Rakesh 创建的主题 » 用于在python中提取脚本标记的regex

使用regex模式 r"\"variants\":\[(.*?)\]"

演示:

from bs4 import BeautifulSoup
import json
import re

s = """<script>var BOLD = BOLD || {};
    BOLD.products = BOLD.products || {};
    BOLD.variant_lookup = BOLD.variant_lookup || {};BOLD.variant_lookup[31066737740] ="womanizer";BOLD.variant_lookup[31066737804] ="womanizer";BOLD.variant_lookup[31066737868] ="womanizer";BOLD.variant_lookup[31066737996] ="womanizer";BOLD.variant_lookup[1509908217881] ="womanizer";BOLD.products["womanizer"] ={"id":8993669708,"title":"Womanizer","variants":[{"id":37834367948,"title":"Black","option1":"Black","option2":null,"option3":null,"sku":"1725205212"}]}
    </script>
"""

soup = BeautifulSoup(s, "html.parser")
src = soup.find("script")
m = re.search(r"\"variants\":\[(.*?)\]", src.string)
if m:
    data = json.loads(m.group(1))
    print(data)

输出:

{u'sku': u'1725205212', u'title': u'Black', u'id': 37834367948L, u'option2': None, u'option3': None, u'option1': u'Black'}
6 年前
回复了 Rakesh 创建的主题 » 查找重复行python

使用 collections.defaultdict

前任:

from collections import defaultdict

res = defaultdict(list)

with open(filename) as infile:
    for line in infile:                      #Iterate each line
        val = line.strip().split()           #Get first word
        res[val[0]].append(line)

for k, v in res.items():
    if len(v) > 1:
        print(v)

输出:

['G1/0/1    fc:3f:db:8d:fd:6c\n', 'G1/0/1    38:63:bb:bb:f8:7d\n']
6 年前
回复了 Rakesh 创建的主题 » python:按字典中的特定键和这些键值的总和分组

这是一种使用简单迭代的方法。

前任:

from collections import defaultdict
dict_ot={"Berlin":4, "London":3,"Madrid":3,"Germany":51, "Others":1, "France":4}
grpBy = [("Germany", "Berlin"), ("Madrid", "Others"), ("London",)]
res = defaultdict(int)
for i in grpBy:
    for j in i:
        res[i] += dict_ot[j]

for k, v in res.items():
    print("{}: {}".format(", ".join(k), v))

输出:

Germany, Berlin: 55
London: 3
Madrid, Others: 4
6 年前
回复了 Rakesh 创建的主题 » python-从文件夹中删除XLSX文件

import os
import glob

path = '/users/user/folder'
for f in glob.iglob(path+'/**/*.xlsx', recursive=True):
    os.remove(f)