小编Sco*_*ter的帖子

Shell one-liner将一行添加到已排序的文件中

我想在文本文件中添加一行,以便对文本文件最初排序的结果进行排序.例如:

cp file tmp; echo "new line" >> tmp; sort tmp > file; rm -f tmp
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢没有临时文件和没有分号(使用管道代替吗?); 使用sed是可以接受的.这是可能的,如果是的话,怎么样?

sorting shell sed pipe

3
推荐指数
2
解决办法
1870
查看次数

查找当前使用的共享内存名称的技术

当打开共享内存对象(使用shm_open)时,为该对象提供名称.每个对象必须具有不同的名称.

有没有办法识别所有当前分配的共享对象的名称?如果是这样,怎么样?

更新:在我看来,这些(就像@HristoLliev所称的那样)系统V共享内存段. ipcs -m报告类似于我期望的段列表,但不显示名称.

linux shared-memory

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

从R中的行名称中查找行号

如何在R中找到特定行名的行号?我找到了一些函数,它们允许您查找特定矩阵值的行号,但不能查找行名.

r rows

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

将值循环到列表中(更改值)

我试图生成一个包含 360 个值的列表,由提供的公式确定。我一直在突出显示的“Elist”部分出现语法错误。这是使用python 3.4。

SRate = [5,5,5]    
Bots = 120
NQueue = 3
TSim = 100
Exp = 2
DDistance = 1
Lambda = 40 # 120/3 = 40

import random

AvgSRate = 5

def Initilization(AvgSRate,Lambda,Exp,x):
    Elist = []
    for a in range(1,361):
        x = (Lambda/(AvgSRate**Exp))*(1+(1/10)*(2*(random.random()) - 1)
        Elist.append(x) # <--- Error is in this line.
    return Expectation_list
Run Code Online (Sandbox Code Playgroud)

python loops list

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

PyBluez的模块对象没有属性'discover_devices'

我是python的新手,我正在努力让Pybluez为我工作.

以下是当我尝试发现蓝牙装置时会发生什么.

import bluetooth
nearby_devices = bluetooth.discover_devices()

Traceback (most recent call last):
   File "<stdin>",line1,in <module>
AttributeError: 'module' object has no attribute 'discover_devices'
Run Code Online (Sandbox Code Playgroud)

我在Windows 8.1,python 2.7.10,pybluez 0.21

python bluetooth

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

使用锚标记Django在网址的另一个页面中链接div

我正在尝试使用Django中的定位标记和URL访问另一个页面中的div。

这是HTML,

<a href="{% url 'anotherpage#show' %}>click here </a>
Run Code Online (Sandbox Code Playgroud)

在另一页中

<div id="show">link to this div </div>
Run Code Online (Sandbox Code Playgroud)

如何show使用网址访问div?

django url anchor

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

Python 日期时间拒绝 2 月 29 日

我在 Windows 11 上使用 Python 3.11 我得到以下结果:

d = 'Mon 29 Feb'
import datetime
datetime.datetime.strptime(d, '%a %d %b')
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    datetime.datetime.strptime(d, '%a %d %b')
  File "C:\Users\pskipwith\AppData\Local\Programs\Python\Python311\Lib\_strptime.py", line 579, in     _strptime_datetime
    return cls(*args)
ValueError: day is out of range for month
Run Code Online (Sandbox Code Playgroud)

但如果我添加年份,比如 2016 年,它就会顺利执行:

d = 'Mon 29 Feb 2016'
datetime.datetime.strptime(d, '%a %d %b %Y')
datetime.datetime(2016, 2, 29, 0, 0)
Run Code Online (Sandbox Code Playgroud)

这是正确的行为吗?

python datetime

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

列表转 csv Python

当我尝试将 Python 列表保存在 csv 中时,csv 包含我想要保存的项目,并用每个字符分隔。

我有一个这样的列表,其中包含链接:

links = ['https://www.portalinmobiliario.com/MLC-2150551226-departamento-los-talaveras-id-117671-_JM#position=1&search_layout=grid&type=item&tracking_id=01bab66e-7cd3-43ce-b3d7-8389260b443d', 
         'https://www.portalinmobiliario.com/MLC-2148268902-departamento-los-espinos-id-116373-_JM#position=2&search_layout=grid&type=item&tracking_id=01bab66e-7cd3-43ce-b3d7-8389260b443d']
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码将其保存到 csv 中:

with open('links.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(links)
Run Code Online (Sandbox Code Playgroud)

我从列表中得到的结果是行中的链接,但列中的每个字符。

  • 如何获得按行分隔但在同一列中的链接?

python csv list python-3.x

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

Python asyncio.sleep SyntaxError

我究竟做错了什么?当我跑这个,

import asyncio
def oneSecond():
    await asyncio.sleep(1)
Run Code Online (Sandbox Code Playgroud)

我明白了:

File "<string>", line 3
await asyncio.sleep(1)
            ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

使用Python 3.3

python syntax-error python-3.x python-asyncio

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

子进程中使用 execl 的 waitpid 与 ECHILD 一起返回 -1?

如果我使用 execl 是可能需要时间完成的子进程,我什么时候需要使用 waitpid?

当我在父级中使用 waitpid 时,它让我的孩子运行,因为 waitpid 的返回值为 0。一段时间后,我试图在另一个函数中使用 waitpid,它用 ECHILD 返回 -1。如果我不确定天气孩子是否完成,我应该什么时候使用 waitpid?

//pid_t Checksum_pid = fork();
Checksum_pid = fork();

if (Checksum_pid == 0)
{
    execl(path, name, argument as needed, ,NULL);
    exit(EXIT_SUCCESS);     
}
else if (Checksum_pid > 0)
{ 
    pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);

    if ( returnValue > 0)
    {
        if (WIFEXITED(childStatus))
        {
            printf("Exit Code: _ WEXITSTATUS(childStatus)") ;               
        }
    }
    else if ( returnValue == 0)
    {
        //Send positive response with routine status running (0x03)
        printf("Child process …
Run Code Online (Sandbox Code Playgroud)

c linux fork os.execl waitpid

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

停止for循环,python

我用for循环编写了一些代码.现在我想在执行循环一次后停止for循环.这是for循环:

for table in tables: #reason for it to do it 12x

    for tr in table.find_all("tr"):
        firstTd = tr.find("td")
        if firstTd and firstTd.has_attr("class") and "indent" in firstTd['class']:
            values = {}
            tds = tr.find_all("td")
            maxVal = tds[1].find("span", class_="wx-value")
            avgVal = tds[2].find("span", class_="wx-value")
            minVal = tds[3].find("span", class_="wx-value")
            if maxVal:
                values['max'] = maxVal.text
            if avgVal:
                values['avg'] = avgVal.text
            if minVal:
                values['min'] = minVal.text
            if len(tds) > 4:
                sumVal = tds[4].find("span", class_="wx-value")
                if sumVal:
                    values['sum'] = sumVal.text
            scrapedData = {}
            scrapedData[firstTd.text] = values
            weatherdata.append(scrapedData) …
Run Code Online (Sandbox Code Playgroud)

python for-loop

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