小编mar*_*eau的帖子

发送带有附件的电子邮件后如何关闭文件?

我有一个循环,它向电子邮件列表中的人员发送带有附件的电子邮件。问题是,当涉及到列表中的最后一个人时,我在电子邮件中附加的文件仍然保持在 Windows 中使用的状态。

for index, row in f.iterrows():
    print (row["ManagerEmail"]+row["filename"])
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['Subject'] = row["filename"] + f" Sales Letter"
    msg.attach(MIMEText(body, 'plain'))
    filename = row["filename"]
    toaddr = row["ManagerEmail"]
    attachment = open(row["filepath"], "rb")
    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
    msg.attach(part)
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
Run Code Online (Sandbox Code Playgroud)

不知何故,我需要在末尾添加一个参数来关闭文件,但我不知道该怎么做。

python email-attachments file-in-use

0
推荐指数
1
解决办法
223
查看次数

我编写了一些代码可以在 Windows 中获取您的分辨率,如何让它以 f 字符串打印出来?

我编写了一些 Python 代码,可以在 Windows 中获取您的分辨率,如何让它以 f 字符串打印出来?

# libraries
import ctypes

# get screen resolution
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), 'x', user32.GetSystemMetrics(1)

# list to store resolution and the x that is between them
items = []

# add resolution to list
for item in screensize:
    resolution = (str(item))
    items.append(item)

# print resolution
for item in items:
    print(item, end='')
Run Code Online (Sandbox Code Playgroud)

它的输出是 1920x1080,但我需要它是这样的,因为它会在其他代码中

print(f'''Resolution: {screen_resolution}''')
User: {getuser()}
Run Code Online (Sandbox Code Playgroud)

因为我正在尝试重新制作 neofetch。除非有更好的方法来解决您的问题?

python windows

0
推荐指数
1
解决办法
49
查看次数

Python String Manipulation

我有一个如下所示的文件.我想要的只是电压,剥离其他所有东西最简单的方法是什么?

Time,Voltage,Peak
0.0,1.003911558621642,3
0.00390625,1.0327467181982755,0
0.0078125,0.9904463156237306,0
0.01171875,0.6867661682528724,0
0.015625,0.6236803073669519,0
0.01953125,0.2934711210503298,0
0.0234375,0.06148933838536881,0
0.02734375,0.07053968550834916,0
0.03125,-0.09041720958299812,0
0.03515625,-0.28273374252040306,0
0.0390625,-0.29775398016603216,0
Run Code Online (Sandbox Code Playgroud)

python csv string parsing file

-1
推荐指数
1
解决办法
1347
查看次数

将整数除以整数给我一个浮点数

其他一切都很好但除了两个

People = input("Number of people: ")
Cookie = input("Numnber of cookies: ")
People = int(People)
Answer = int(Cookie) / int(People)
Remain = int(Cookie) % int(People)
print ("Cookies per person: ", Answer)
print ("Cookies returning to the jar: ", Remain)
Run Code Online (Sandbox Code Playgroud)

使用两个变量4(people)和11(cookies)运行代码会返回:

Number of people: 4
Number of cookies: 11
Cookies per person:  2.75
Cookies returning to the jar:  3
Run Code Online (Sandbox Code Playgroud)

如何将2.75更改为2?

Python 3.3

python python-3.x

-1
推荐指数
1
解决办法
111
查看次数

给定一个可迭代的,如何在每个可能的组合中应用函数?

给定迭代[A, B, C]和函数f(x)我想得到以下内容:

[  A,     B,     C]  
[  A,     B,   f(C)]  
[  A,   f(B),    C]
[  A,   f(B),  f(C)]
[f(A),    B,     C]
[f(A),    B,   f(C)]
[f(A),  f(B),    C]
[f(A),  f(B),  f(C)]
Run Code Online (Sandbox Code Playgroud)

不幸的是我在itertools模块中找不到任何合适的东西.

python

-1
推荐指数
1
解决办法
62
查看次数

有没有办法在python中查看float的内部表示?

python浮点教程中,

在碱2中,1/10是无限重复的部分

0.0001100110011001100110011001100110011001100110011 ...

如何让python解释器打印这种内心表示?repr(0.1)没有帮助.

python floating-point

-1
推荐指数
1
解决办法
886
查看次数

按键排序defaultdict?

我按时间戳将对象列表分组在一起:

object_list = [
{
    timestamp: datetime.strptime("01/01/2014", "%d/%m/%y"),
},
{
    timestamp: datetime.strptime("12/05/2014", "%d/%m/%y"),
},
{
    timestamp: datetime.strptime("03/01/2014", "%d/%m/%y"),
},
{
    timestamp: datetime.strptime("01/01/2014", "%d/%m/%y"),
}]

date_grouped_objects = defaultdict(list)

for obj in object_list:
    date_grouped_objects[obj.timestamp].append(obj)
Run Code Online (Sandbox Code Playgroud)

这给了我我想要的东西,一个按时间戳属性组合在一起的对象列表.

问题:我现在想要通过键(时间戳)对date_grouped_objects进行排序,但是不清楚如何使用sorted来实现这一点?因此,最近日期的最多组将是最后一个

所以我要追求的是:

[
    ["01/01/2014"] = [...],
    ["03/01/2014"] = [...],
    ["12/05/2014"] = [...],
]
Run Code Online (Sandbox Code Playgroud)

键实际上是日期对象,而不是字符串.

python

-1
推荐指数
1
解决办法
7475
查看次数

排序一个字典...有效的方法吗?

我有这样的问题,我有这样的乘客的字典:

passengers = {
              1: {'name': 'Foo', 'lastname': 'Bar', 'exclusive': True},
              2: {'name': 'John', 'lastname': 'Doe'},
              3: {'name': 'Rocky', 'lastname': 'Balboa', 'exclusive': True},
              4: {'name': 'Mohammed', 'lastname': 'Smith'}
             }
Run Code Online (Sandbox Code Playgroud)

而且我需要首先打印出这些项目的结果,然后是其余的:
这是所需的输出

List of passengers:
===================
1.- Foo Bar
2.- Rocky Balboa
3.- John Doe
4.- Mohammed Smith
Run Code Online (Sandbox Code Playgroud)

我试过了collections.deque,我找不到任何适合我的东西,直到我想出这个功能:

def prioritize_passengers(dictionary):
    priority_list = []
    normal_list = []
    sorted_list = []

    for key, item in dictionary.iteritems():
        if 'exclusive' in item:
            priority_list.append(key)
        else:
            normal_list.append(key)

    sorted_list = priority_list + normal_list

    return sorted_list …
Run Code Online (Sandbox Code Playgroud)

python dictionary list python-2.7

-1
推荐指数
1
解决办法
68
查看次数

删除超过X天的目录?

我决定去python,因为我正在学习Python,所以我尽可能地使用Powershell.

我有这个理论,但似乎os.stat不能列出一个列表,但只有一个字符串或int.现在我只是在打印之前打印删除的东西.

import os
import time

path = "\\\\path\\to\\videoroot\\"
now = time.time()
old = now - 1296000

for root, dirs, files in os.walk(path, topdown=False):
    if time.ctime(os.path.getmtime(dirs) < old:
        print (dirs)
Run Code Online (Sandbox Code Playgroud)

输出/错误消息:

return os.stat(filename).st_mtime
    TypeError: argument should be string, bytes or integer, not list
Run Code Online (Sandbox Code Playgroud)

python

-1
推荐指数
1
解决办法
2584
查看次数

max()大嵌套列表

我在处理一对长列表对的最大值时遇到了一个非常奇怪的问题,例如

[
    [(0, 1), (1, 1), (2, 1), (3, 4), (4, 1), (5, 1), (6, 1),...,(141,3)],
    ..., 
    [(12, 1), (36, 1), (91, 1), (92, 1), (110, 1),..., (180, 1)]
]
Run Code Online (Sandbox Code Playgroud)

我试图获得所有对中第一个元素的最大值.从语法上说,我在做:

max([max(x) for x in list])[0]
Run Code Online (Sandbox Code Playgroud)

实际上返回正确的数字,如果列表短于281个列表.事实上,一旦列表超过280,我就会收到此消息

ValueError: max() arg is an empty sequence
Run Code Online (Sandbox Code Playgroud)

所以,一长串清单

max([max(x) for x in list[0:280]])[0]
Run Code Online (Sandbox Code Playgroud)

这很好,而

max([max(x) for x in list[0:281]])[0]
Run Code Online (Sandbox Code Playgroud)

休息.

我在这里做错了吗?

python list max nested-lists

-1
推荐指数
1
解决办法
343
查看次数