小编Can*_*ame的帖子

在scrapy中剥离\n\t\r \n

我试图用scrapy蜘蛛剥离\ r \n\t字符,然后制作一个json文件.

我有一个"描述"对象,它充满了新的行,并没有做我想要的:将每个描述与标题相匹配.

我尝试使用map(unicode.strip()),但它并没有真正起作用.作为scrapy的新手我不知道是否有另一种更简单的方法或者map unicode是如何工作的.

这是我的代码:

def parse(self, response):
    for sel in response.xpath('//div[@class="d-grid-main"]'):
        item = xItem()
        item['TITLE'] = sel.xpath('xpath').extract()
        item['DESCRIPTION'] = map(unicode.strip, sel.xpath('//p[@class="class-name"]/text()').extract())
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

item['DESCRIPTION'] = str(sel.xpath('//p[@class="class-name"]/text()').extract()).strip()
Run Code Online (Sandbox Code Playgroud)

但它引发了一个错误.什么是最好的方式?

python unicode scrapy

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

如何管理graphviz中节点之间的距离?

我试图绘制一个具有3级节点的图形,各级之间的距离相等.然而,graphviz以某种方式决定中间水平和底部水平之间的距离应远大于顶部和中间水平之间的距离.有任何解决这个问题的方法吗?

这是我的代码:

digraph g{
    rankdir="LR";
    graph [pad="0.5", ranksep="0.525", nodesep="3"];
    splines=false;
    node[shape = square];
    edge[style=invis];
    subgraph cluster_3 {
        color=invis;
        a1->a2->a3->a4->a5->a6->a7;
    }
    subgraph cluster_2 {
        color=invis;
        a->b->c->d->e->f->g;
    }
    subgraph cluster_1 {
        color=invis;
        1->2->3->4->5->6->7;
    }
    "a1" [label="1'"];
    "a2" [label="2'"];
    "a3" [label="3'"];
    "a4" [label="4'"];
    "a5" [label="5'"];
    "a6" [label="6'"];
    "a7" [label="7'"];
    edge[style=solid, constraint=false];
    a->1[arrowhead=none, arrowtail=none];
    a->2[arrowhead=none, arrowtail=none];
    a->3[arrowhead=none, arrowtail=none];
    a->a1[arrowhead=none, arrowtail=none];
    a->a2[arrowhead=none, arrowtail=none];
    a->a3[arrowhead=none, arrowtail=none];
    b->1[arrowhead=none, arrowtail=none];
    b->3[arrowhead=none, arrowtail=none];
    b->7[arrowhead=none, arrowtail=none];
    b->a1[arrowhead=none, arrowtail=none];
    b->a3[arrowhead=none, arrowtail=none];
    b->a7[arrowhead=none, arrowtail=none];
    c->2[arrowhead=none, arrowtail=none];
    c->6[arrowhead=none, arrowtail=none];
    c->7[arrowhead=none, arrowtail=none];
    c->a2[arrowhead=none, arrowtail=none]; …
Run Code Online (Sandbox Code Playgroud)

dot graphviz

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

模板好友功能的前向声明

请考虑以下完全正常的代码片段:

class A
{
private:
    int d;
public:
    A(int n){ d = n;}
    friend int foo(A a);
};

int foo(A a)
{
    return a.d;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试为类使用模板时,我需要转发声明友元函数才能运行,如下所示:

template <typename T>
class B;

template <typename T>
T foof(B<T> a);


template <typename T>
class B
{
private:
    T d;
public:
    B(T n){ d = n;}
    friend T foof<>(B<T> a);
};

template <typename T>
T foof(B<T> a)
{
    return a.d;
}
Run Code Online (Sandbox Code Playgroud)

为什么前面的声明在第二个例子中是必要的而不是在第一个例子中?另外,为什么我必须<>在B类中加入foof声明?为什么在模板内声明它是不够的?我试图理解这些东西是如何工作的,所以当我需要使用它时,我不必盲目地记住这种代码.

谢谢

c++ templates friend

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

使用字符串索引数组(C)

我有一个无符号整数数组,每个整数对应一个12个字符的字符串,可以包含4个不同的字符,即'A','B','C','D'.因此,该阵列将包含4 ^ 12 = 16777216个元素.数组中元素的排序是任意的; 我可以选择哪一个对应于每个字符串.到目前为止,我已经实现了这一点:

unsigned int my_array[16777216];
char my_string[12];
int index = string_to_index(my_string);

my_array[index] = ...;
Run Code Online (Sandbox Code Playgroud)

string_to_index()简单地为每个字符分配2位,如下所示:A - > 00,B - > 01,C - > 10,D - > 11例如,ABCDABCDABCD对应索引(000110110001101100011011)2 =(1776411)10

但是,我知道一个事实是,用于访问数组的每个字符串都是前一个字符串,它向左移动了一个新的最后一个字符.例如,在我使用ABCDABCDABCD访问后,下一次访问将使用BCDABCDABCDA或BCDABCDABCDB,BCDABCDABCDC,BCDABCDABCDD.

所以我的问题是:有没有更好的方法来实现string_to_index函数来考虑最后这个事实,以便连续访问的元素在数组中更接近?我希望通过这样做来提高我的缓存性能.

编辑:也许我不太清楚:我正在寻找一个完全不同的字符串来索引对应方案,以便ABCDABCDABCD和BCDABCDABCDA的索引更接近.

c arrays string caching

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

使用错误的Scala版本创建Spark应用程序

我按照这里的说明操作:https://spark.apache.org/docs/latest/quick-start.html创建一个将在本地独立Spark构建上运行的简单应用程序.

在我的系统中,我有Scala 2.9.2和sbt 0.13.7.当我写下simple.sbt以下内容时:

scalaVersion := "2.9.2"

使用后sbt package,我收到错误: sbt.ResolveException: unresolved dependency: org.apache.spark#spark-core_2.9.2;1.3.1: not found

但是,当我写信时simple.sbt:

scalaVersion := "2.10.4"

sbt成功运行,应用程序在Spark上运行正常.

怎么会发生这种情况,因为我的系统上没有scala 2.10.4?

scala sbt apache-spark

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

静态成员函数是可重入的吗?

正如标题所说:

如果我有一个带有静态成员函数的类,它本身不包含静态变量,我可以考虑该成员函数可重入吗?

c++ reentrancy

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

标签 统计

c++ ×2

apache-spark ×1

arrays ×1

c ×1

caching ×1

dot ×1

friend ×1

graphviz ×1

python ×1

reentrancy ×1

sbt ×1

scala ×1

scrapy ×1

string ×1

templates ×1

unicode ×1