标签归档:python-2.7

在Python中查找数字的所有因子的最有效方法是什么?

问题:在Python中查找数字的所有因子的最有效方法是什么?

有人可以向我解释一种在Python(2.7)中找到一个数字的所有因子的有效方法吗?

我可以创建一个算法来执行此操作,但是我认为它的编码很差,并且花费大量时间才能生成大量结果。

Can someone explain to me an efficient way of finding all the factors of a number in Python (2.7)?

I can create an algorithm to do this, but I think it is poorly coded and takes too long to produce a result for a large number.


回答 0

from functools import reduce

def factors(n):    
    return set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

这将很快返回所有因素n

为什么以平方根为上限?

sqrt(x) * sqrt(x) = x。因此,如果两个因素相同,则它们都是平方根。如果使一个因子变大,则必须使另一个因子变小。这意味着这两个之一将始终小于或等于sqrt(x),因此您只需要搜索到该点即可找到两个匹配因子之一。然后,您可以使用x / fac1获取fac2

reduce(list.__add__, ...)走的小名单[fac1, fac2],并在一个长长的清单一起加入他们。

[i, n/i] for i in range(1, int(sqrt(n)) + 1) if n % i == 0返回两个因素,如果当你除以其余n由较小的一个是零(它并不需要检查较大的一个过;它只是获取除以n由较小的一个。)

set(...)在外面摆脱重复,这仅发生于完美的正方形。对于n = 4,它将返回2两次,因此set摆脱其中之一。

from functools import reduce

def factors(n):    
    return set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

This will return all of the factors, very quickly, of a number n.

Why square root as the upper limit?

sqrt(x) * sqrt(x) = x. So if the two factors are the same, they’re both the square root. If you make one factor bigger, you have to make the other factor smaller. This means that one of the two will always be less than or equal to sqrt(x), so you only have to search up to that point to find one of the two matching factors. You can then use x / fac1 to get fac2.

The reduce(list.__add__, ...) is taking the little lists of [fac1, fac2] and joining them together in one long list.

The [i, n/i] for i in range(1, int(sqrt(n)) + 1) if n % i == 0 returns a pair of factors if the remainder when you divide n by the smaller one is zero (it doesn’t need to check the larger one too; it just gets that by dividing n by the smaller one.)

The set(...) on the outside is getting rid of duplicates, which only happens for perfect squares. For n = 4, this will return 2 twice, so set gets rid of one of them.


回答 1

@agf提出的解决方案很棒,但是通过检查奇偶校验,可以将任意奇数的运行时间缩短约50%。由于奇数的因子本身始终都是奇数,因此在处理奇数时不必检查它们。

我刚刚开始解决欧拉计划困惑了自己。在某些问题中,在两个嵌套for循环内调用除数检查,因此该功能的性能至关重要。

将这一事实与agf的出色解决方案相结合,我最终获得了以下功能:

from math import sqrt
def factors(n):
        step = 2 if n%2 else 1
        return set(reduce(list.__add__,
                    ([i, n//i] for i in range(1, int(sqrt(n))+1, step) if n % i == 0)))

但是,对于较小的数字(〜<100),此更改带来的额外开销可能导致该功能花费更长的时间。

我进行了一些测试以检查速度。下面是使用的代码。为了产生不同的情节,我相应地进行了更改X = range(1,100,1)

import timeit
from math import sqrt
from matplotlib.pyplot import plot, legend, show

def factors_1(n):
    step = 2 if n%2 else 1
    return set(reduce(list.__add__,
                ([i, n//i] for i in range(1, int(sqrt(n))+1, step) if n % i == 0)))

def factors_2(n):
    return set(reduce(list.__add__,
                ([i, n//i] for i in range(1, int(sqrt(n)) + 1) if n % i == 0)))

X = range(1,100000,1000)
Y = []
for i in X:
    f_1 = timeit.timeit('factors_1({})'.format(i), setup='from __main__ import factors_1', number=10000)
    f_2 = timeit.timeit('factors_2({})'.format(i), setup='from __main__ import factors_2', number=10000)
    Y.append(f_1/f_2)
plot(X,Y, label='Running time with/without parity check')
legend()
show()

X =范围(1,100,1)

这里没有显着差异,但是数量更大时,优势显而易见:

X =范围(1,100000,1000)(仅奇数)

X = range(2,100000,100)(仅偶数)

X = range(1,100000,1001)(交替奇偶校验)

The solution presented by @agf is great, but one can achieve ~50% faster run time for an arbitrary odd number by checking for parity. As the factors of an odd number always are odd themselves, it is not necessary to check these when dealing with odd numbers.

I’ve just started solving Project Euler puzzles myself. In some problems, a divisor check is called inside two nested for loops, and the performance of this function is thus essential.

Combining this fact with agf’s excellent solution, I’ve ended up with this function:

from math import sqrt
def factors(n):
        step = 2 if n%2 else 1
        return set(reduce(list.__add__,
                    ([i, n//i] for i in range(1, int(sqrt(n))+1, step) if n % i == 0)))

However, on small numbers (~ < 100), the extra overhead from this alteration may cause the function to take longer.

I ran some tests in order to check the speed. Below is the code used. To produce the different plots, I altered the X = range(1,100,1) accordingly.

import timeit
from math import sqrt
from matplotlib.pyplot import plot, legend, show

def factors_1(n):
    step = 2 if n%2 else 1
    return set(reduce(list.__add__,
                ([i, n//i] for i in range(1, int(sqrt(n))+1, step) if n % i == 0)))

def factors_2(n):
    return set(reduce(list.__add__,
                ([i, n//i] for i in range(1, int(sqrt(n)) + 1) if n % i == 0)))

X = range(1,100000,1000)
Y = []
for i in X:
    f_1 = timeit.timeit('factors_1({})'.format(i), setup='from __main__ import factors_1', number=10000)
    f_2 = timeit.timeit('factors_2({})'.format(i), setup='from __main__ import factors_2', number=10000)
    Y.append(f_1/f_2)
plot(X,Y, label='Running time with/without parity check')
legend()
show()

X = range(1,100,1)

No significant difference here, but with bigger numbers, the advantage is obvious:

X = range(1,100000,1000) (only odd numbers)

X = range(2,100000,100) (only even numbers)

X = range(1,100000,1001) (alternating parity)


回答 2

AGF的答案确实很酷。我想看看是否可以重写它以避免使用reduce()。这是我想出的:

import itertools
flatten_iter = itertools.chain.from_iterable
def factors(n):
    return set(flatten_iter((i, n//i) 
                for i in range(1, int(n**0.5)+1) if n % i == 0))

我还尝试了使用棘手的生成器功能的版本:

def factors(n):
    return set(x for tup in ([i, n//i] 
                for i in range(1, int(n**0.5)+1) if n % i == 0) for x in tup)

我通过计算来计时:

start = 10000000
end = start + 40000
for n in range(start, end):
    factors(n)

我运行了一次以让Python对其进行编译,然后在time(1)命令下运行了三次,并保持了最佳时间。

  • 减少版本:11.58秒
  • itertools版本:11.49秒
  • 棘手的版本:​​11.12秒

注意itertools版本正在构建一个元组,并将其传递给flatten_iter()。如果我更改代码以构建列表,则它会稍微降低速度:

  • iterools(列表)版本:11.62秒

我相信棘手的生成器函数版本是Python中最快的。但这并没有比简化版本快很多,根据我的测量,速度大约快4%。

agf’s answer is really quite cool. I wanted to see if I could rewrite it to avoid using reduce(). This is what I came up with:

import itertools
flatten_iter = itertools.chain.from_iterable
def factors(n):
    return set(flatten_iter((i, n//i) 
                for i in range(1, int(n**0.5)+1) if n % i == 0))

I also tried a version that uses tricky generator functions:

def factors(n):
    return set(x for tup in ([i, n//i] 
                for i in range(1, int(n**0.5)+1) if n % i == 0) for x in tup)

I timed it by computing:

start = 10000000
end = start + 40000
for n in range(start, end):
    factors(n)

I ran it once to let Python compile it, then ran it under the time(1) command three times and kept the best time.

  • reduce version: 11.58 seconds
  • itertools version: 11.49 seconds
  • tricky version: 11.12 seconds

Note that the itertools version is building a tuple and passing it to flatten_iter(). If I change the code to build a list instead, it slows down slightly:

  • iterools (list) version: 11.62 seconds

I believe that the tricky generator functions version is the fastest possible in Python. But it’s not really much faster than the reduce version, roughly 4% faster based on my measurements.


回答 3

AGF回答的另一种方法:

def factors(n):    
    result = set()
    for i in range(1, int(n ** 0.5) + 1):
        div, mod = divmod(n, i)
        if mod == 0:
            result |= {i, div}
    return result

An alternative approach to agf’s answer:

def factors(n):    
    result = set()
    for i in range(1, int(n ** 0.5) + 1):
        div, mod = divmod(n, i)
        if mod == 0:
            result |= {i, div}
    return result

回答 4

这是@agf解决方案的替代方法,该解决方案以更Python的样式实现相同的算法:

def factors(n):
    return set(
        factor for i in range(1, int(n**0.5) + 1) if n % i == 0
        for factor in (i, n//i)
    )

此解决方案在没有导入的Python 2和Python 3中均可使用,并且可读性更高。我还没有测试这种方法的性能,但是渐近地它应该是相同的,并且如果性能是一个严重的问题,那么这两种解决方案都不是最优的。

Here’s an alternative to @agf’s solution which implements the same algorithm in a more pythonic style:

def factors(n):
    return set(
        factor for i in range(1, int(n**0.5) + 1) if n % i == 0
        for factor in (i, n//i)
    )

This solution works in both Python 2 and Python 3 with no imports and is much more readable. I haven’t tested the performance of this approach, but asymptotically it should be the same, and if performance is a serious concern, neither solution is optimal.


回答 5

SymPy中有一种称为强度因子的行业优势算法:

>>> from sympy import factorint
>>> factorint(2**70 + 3**80) 
{5: 2,
 41: 1,
 101: 1,
 181: 1,
 821: 1,
 1597: 1,
 5393: 1,
 27188665321L: 1,
 41030818561L: 1}

这花了不到一分钟的时间。它在多种方法之间切换。请参阅上面链接的文档。

考虑到所有主要因素,可以轻松构建所有其他因素。


请注意,即使允许接受的答案运行足够长的时间(即一个永恒的时间)来分解上述数字,但对于某些较大的数字,它将失败,例如以下示例。这是由于马虎int(n**0.5)。例如,当n = 10000000000000079**2我们有

>>> int(n**0.5)
10000000000000078L

由于10000000000000079是质数,因此可接受的答案的算法将永远找不到此因子。请注意,这不仅是一对一的。对于更大的数字,它将更多。因此,最好避免在此类算法中使用浮点数。

There is an industry-strength algorithm in SymPy called factorint:

>>> from sympy import factorint
>>> factorint(2**70 + 3**80) 
{5: 2,
 41: 1,
 101: 1,
 181: 1,
 821: 1,
 1597: 1,
 5393: 1,
 27188665321L: 1,
 41030818561L: 1}

This took under a minute. It switches among a cocktail of methods. See the documentation linked above.

Given all the prime factors, all other factors can be built easily.


Note that even if the accepted answer was allowed to run for long enough (i.e. an eternity) to factor the above number, for some large numbers it will fail, such the following example. This is due to the sloppy int(n**0.5). For example, when n = 10000000000000079**2, we have

>>> int(n**0.5)
10000000000000078L

Since 10000000000000079 is a prime, the accepted answer’s algorithm will never find this factor. Note that it’s not just an off-by-one; for larger numbers it will be off by more. For this reason it’s better to avoid floating-point numbers in algorithms of this sort.


回答 6

对于n高达10 ** 16(甚至更多)的情况,这是一个快速的纯Python 3.6解决方案,

from itertools import compress

def primes(n):
    """ Returns  a list of primes < n for n > 2 """
    sieve = bytearray([True]) * (n//2)
    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            sieve[i*i//2::i] = bytearray((n-i*i-1)//(2*i)+1)
    return [2,*compress(range(3,n,2), sieve[1:])]

def factorization(n):
    """ Returns a list of the prime factorization of n """
    pf = []
    for p in primeslist:
      if p*p > n : break
      count = 0
      while not n % p:
        n //= p
        count += 1
      if count > 0: pf.append((p, count))
    if n > 1: pf.append((n, 1))
    return pf

def divisors(n):
    """ Returns an unsorted list of the divisors of n """
    divs = [1]
    for p, e in factorization(n):
        divs += [x*p**k for k in range(1,e+1) for x in divs]
    return divs

n = 600851475143
primeslist = primes(int(n**0.5)+1) 
print(divisors(n))

For n up to 10**16 (maybe even a bit more), here is a fast pure Python 3.6 solution,

from itertools import compress

def primes(n):
    """ Returns  a list of primes < n for n > 2 """
    sieve = bytearray([True]) * (n//2)
    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            sieve[i*i//2::i] = bytearray((n-i*i-1)//(2*i)+1)
    return [2,*compress(range(3,n,2), sieve[1:])]

def factorization(n):
    """ Returns a list of the prime factorization of n """
    pf = []
    for p in primeslist:
      if p*p > n : break
      count = 0
      while not n % p:
        n //= p
        count += 1
      if count > 0: pf.append((p, count))
    if n > 1: pf.append((n, 1))
    return pf

def divisors(n):
    """ Returns an unsorted list of the divisors of n """
    divs = [1]
    for p, e in factorization(n):
        divs += [x*p**k for k in range(1,e+1) for x in divs]
    return divs

n = 600851475143
primeslist = primes(int(n**0.5)+1) 
print(divisors(n))

回答 7

对afg&eryksun解决方案的进一步改进。下面的代码返回所有因素的排序列表,而不改变运行时的渐进复杂性:

    def factors(n):    
        l1, l2 = [], []
        for i in range(1, int(n ** 0.5) + 1):
            q,r = n//i, n%i     # Alter: divmod() fn can be used.
            if r == 0:
                l1.append(i) 
                l2.append(q)    # q's obtained are decreasing.
        if l1[-1] == l2[-1]:    # To avoid duplication of the possible factor sqrt(n)
            l1.pop()
        l2.reverse()
        return l1 + l2

想法:不要使用list.sort()函数来获取有序列表,从而使nlog(n)变得复杂。在l2上使用list.reverse()更快,这会增加O(n)的复杂度。(这就是python的制作方法。)在l2.reverse()之后,可以将l2附加到l1以获得因子的排序列表。

注意,l1包含不断增加的i -s。l2包含q -s递减。这就是使用上述想法的原因。

Further improvement to afg & eryksun’s solution. The following piece of code returns a sorted list of all the factors without changing run time asymptotic complexity:

    def factors(n):    
        l1, l2 = [], []
        for i in range(1, int(n ** 0.5) + 1):
            q,r = n//i, n%i     # Alter: divmod() fn can be used.
            if r == 0:
                l1.append(i) 
                l2.append(q)    # q's obtained are decreasing.
        if l1[-1] == l2[-1]:    # To avoid duplication of the possible factor sqrt(n)
            l1.pop()
        l2.reverse()
        return l1 + l2

Idea: Instead of using the list.sort() function to get a sorted list which gives nlog(n) complexity; It is much faster to use list.reverse() on l2 which takes O(n) complexity. (That’s how python is made.) After l2.reverse(), l2 may be appended to l1 to get the sorted list of factors.

Notice, l1 contains i-s which are increasing. l2 contains q-s which are decreasing. Thats the reason behind using the above idea.


回答 8

我用timeit尝试了这些绝妙的答案中的大多数,以比较它们的效率与我的简单功能,但我不断看到我的表现优于此处列出的那些。我想分享一下,看看大家都怎么想。

def factors(n):
    results = set()
    for i in xrange(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            results.add(i)
            results.add(int(n/i))
    return results

在编写本文时,您必须导入数学以进行测试,但是用n **。5替换math.sqrt(n)也应同样有效。我不会浪费时间检查重复项,因为无论如何重复项都不能存在于集合中。

I’ve tried most of these wonderful answers with timeit to compare their efficiency versus my simple function and yet I constantly see mine outperform those listed here. I figured I’d share it and see what you all think.

def factors(n):
    results = set()
    for i in xrange(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            results.add(i)
            results.add(int(n/i))
    return results

As it’s written you’ll have to import math to test, but replacing math.sqrt(n) with n**.5 should work just as well. I don’t bother wasting time checking for duplicates because duplicates can’t exist in a set regardless.


回答 9

这是另一个没有reduce的替代方法,可以很好地处理大量数据。它用于sum拉平列表。

def factors(n):
    return set(sum([[i, n//i] for i in xrange(1, int(n**0.5)+1) if not n%i], []))

Here is another alternate without reduce that performs well with large numbers. It uses sum to flatten the list.

def factors(n):
    return set(sum([[i, n//i] for i in xrange(1, int(n**0.5)+1) if not n%i], []))

回答 10

确保抓取的数字大于sqrt(number_to_factor)不寻常数字(例如99,其具有3 * 3 * 11和)floor sqrt(99)+1 == 10

import math

def factor(x):
  if x == 0 or x == 1:
    return None
  res = []
  for i in range(2,int(math.floor(math.sqrt(x)+1))):
    while x % i == 0:
      x /= i
      res.append(i)
  if x != 1: # Unusual numbers
    res.append(x)
  return res

Be sure to grab the number larger than sqrt(number_to_factor) for unusual numbers like 99 which has 3*3*11 and floor sqrt(99)+1 == 10.

import math

def factor(x):
  if x == 0 or x == 1:
    return None
  res = []
  for i in range(2,int(math.floor(math.sqrt(x)+1))):
    while x % i == 0:
      x /= i
      res.append(i)
  if x != 1: # Unusual numbers
    res.append(x)
  return res

回答 11

查找数量因子的最简单方法:

def factors(x):
    return [i for i in range(1,x+1) if x%i==0]

The simplest way of finding factors of a number:

def factors(x):
    return [i for i in range(1,x+1) if x%i==0]

回答 12

这是一个示例,如果您想使用质数更快。这些列表很容易在Internet上找到。我在代码中添加了注释。

# http://primes.utm.edu/lists/small/10000.txt
# First 10000 primes

_PRIMES = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 
        31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 
        73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
        127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 
        179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 
        233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 
        283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 
        353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 
        419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 
        467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 
        547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 
        607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 
        661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 
        739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 
        811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 
        877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 
        947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 
# Mising a lot of primes for the purpose of the example
)


from bisect import bisect_left as _bisect_left
from math import sqrt as _sqrt


def get_factors(n):
    assert isinstance(n, int), "n must be an integer."
    assert n > 0, "n must be greather than zero."
    limit = pow(_PRIMES[-1], 2)
    assert n <= limit, "n is greather then the limit of {0}".format(limit)
    result = set((1, n))
    root = int(_sqrt(n))
    primes = [t for t in get_primes_smaller_than(root + 1) if not n % t]
    result.update(primes)  # Add all the primes factors less or equal to root square
    for t in primes:
        result.update(get_factors(n/t))  # Add all the factors associted for the primes by using the same process
    return sorted(result)


def get_primes_smaller_than(n):
    return _PRIMES[:_bisect_left(_PRIMES, n)]

Here is an example if you want to use the primes number to go a lot faster. These lists are easy to find on the internet. I added comments in the code.

# http://primes.utm.edu/lists/small/10000.txt
# First 10000 primes

_PRIMES = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 
        31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 
        73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
        127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 
        179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 
        233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 
        283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 
        353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 
        419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 
        467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 
        547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 
        607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 
        661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 
        739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 
        811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 
        877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 
        947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 
# Mising a lot of primes for the purpose of the example
)


from bisect import bisect_left as _bisect_left
from math import sqrt as _sqrt


def get_factors(n):
    assert isinstance(n, int), "n must be an integer."
    assert n > 0, "n must be greather than zero."
    limit = pow(_PRIMES[-1], 2)
    assert n <= limit, "n is greather then the limit of {0}".format(limit)
    result = set((1, n))
    root = int(_sqrt(n))
    primes = [t for t in get_primes_smaller_than(root + 1) if not n % t]
    result.update(primes)  # Add all the primes factors less or equal to root square
    for t in primes:
        result.update(get_factors(n/t))  # Add all the factors associted for the primes by using the same process
    return sorted(result)


def get_primes_smaller_than(n):
    return _PRIMES[:_bisect_left(_PRIMES, n)]

回答 13

一种可能比此处介绍的算法更有效的算法(尤其是如果其中的主要因素较少时n)。诀窍是调整每次发现主要因素时需要进行试验划分的限制

def factors(n):
    '''
    return prime factors and multiplicity of n
    n = p0^e0 * p1^e1 * ... * pk^ek encoded as
    res = [(p0, e0), (p1, e1), ..., (pk, ek)]
    '''

    res = []

    # get rid of all the factors of 2 using bit shifts
    mult = 0
    while not n & 1:
        mult += 1
        n >>= 1
    if mult != 0:
        res.append((2, mult))

    limit = round(sqrt(n))
    test_prime = 3
    while test_prime <= limit:
        mult = 0
        while n % test_prime == 0:
            mult += 1
            n //= test_prime
        if mult != 0:
            res.append((test_prime, mult))
            if n == 1:              # only useful if ek >= 3 (ek: multiplicity
                break               # of the last prime) 
            limit = round(sqrt(n))  # adjust the limit
        test_prime += 2             # will often not be prime...
    if n != 1:
        res.append((n, 1))
    return res

当然,这仍然是审判部门,仅此而已。因此,其效率仍然非常有限(尤其是对于没有小除数的大量用户)。

这是python3; 划分//应该是您唯一需要适应python 2(add from __future__ import division)的东西。

a potentially more efficient algorithm than the ones presented here already (especially if there are small prime factons in n). the trick here is to adjust the limit up to which trial division is needed every time prime factors are found:

def factors(n):
    '''
    return prime factors and multiplicity of n
    n = p0^e0 * p1^e1 * ... * pk^ek encoded as
    res = [(p0, e0), (p1, e1), ..., (pk, ek)]
    '''

    res = []

    # get rid of all the factors of 2 using bit shifts
    mult = 0
    while not n & 1:
        mult += 1
        n >>= 1
    if mult != 0:
        res.append((2, mult))

    limit = round(sqrt(n))
    test_prime = 3
    while test_prime <= limit:
        mult = 0
        while n % test_prime == 0:
            mult += 1
            n //= test_prime
        if mult != 0:
            res.append((test_prime, mult))
            if n == 1:              # only useful if ek >= 3 (ek: multiplicity
                break               # of the last prime) 
            limit = round(sqrt(n))  # adjust the limit
        test_prime += 2             # will often not be prime...
    if n != 1:
        res.append((n, 1))
    return res

this is of course still trial division and nothing more fancy. and therefore still very limited in its efficiency (especially for big numbers without small divisors).

this is python3; the division // should be the only thing you need to adapt for python 2 (add from __future__ import division).


回答 14

使用set(...)会使代码稍微慢一些,并且仅在检查平方根时才真正需要。这是我的版本:

def factors(num):
    if (num == 1 or num == 0):
        return []
    f = [1]
    sq = int(math.sqrt(num))
    for i in range(2, sq):
        if num % i == 0:
            f.append(i)
            f.append(num/i)
    if sq > 1 and num % sq == 0:
        f.append(sq)
        if sq*sq != num:
            f.append(num/sq)
    return f

if sq*sq != num:对于12之类的数字,条件是必需的,其中平方根不是整数,但是平方根的底数是一个因子。

请注意,此版本本身不返回数字,但是如果需要,可以轻松解决。输出也未排序。

我将其定为在所有数字1-200上运行10000次,并在所有数字1-5000上运行100次。它的性能优于我测试过的所有其他版本,包括dansalmo,Jason Schorn,oxrock,agf,steveha和eryksun的解决方案,尽管oxrock最接近。

Using set(...) makes the code slightly slower, and is only really necessary for when you check the square root. Here’s my version:

def factors(num):
    if (num == 1 or num == 0):
        return []
    f = [1]
    sq = int(math.sqrt(num))
    for i in range(2, sq):
        if num % i == 0:
            f.append(i)
            f.append(num/i)
    if sq > 1 and num % sq == 0:
        f.append(sq)
        if sq*sq != num:
            f.append(num/sq)
    return f

The if sq*sq != num: condition is necessary for numbers like 12, where the square root is not an integer, but the floor of the square root is a factor.

Note that this version doesn’t return the number itself, but that is an easy fix if you want it. The output also isn’t sorted.

I timed it running 10000 times on all numbers 1-200 and 100 times on all numbers 1-5000. It outperforms all the other versions I tested, including dansalmo’s, Jason Schorn’s, oxrock’s, agf’s, steveha’s, and eryksun’s solutions, though oxrock’s is by far the closest.


回答 15

您的最大因数不超过您的数字,所以,假设

def factors(n):
    factors = []
    for i in range(1, n//2+1):
        if n % i == 0:
            factors.append (i)
    factors.append(n)

    return factors

瞧!

your max factor is not more than your number, so, let’s say

def factors(n):
    factors = []
    for i in range(1, n//2+1):
        if n % i == 0:
            factors.append (i)
    factors.append(n)

    return factors

voilá!


回答 16

 import math

    '''
    I applied finding prime factorization to solve this. (Trial Division)
    It's not complicated
    '''


    def generate_factors(n):
        lower_bound_check = int(math.sqrt(n))  # determine lowest bound divisor range [16 = 4]
        factors = set()  # store factors
        for divisors in range(1, lower_bound_check + 1):  # loop [1 .. 4]
            if n % divisors == 0:
                factors.add(divisors)  # lower bound divisor is found 16 [ 1, 2, 4]
                factors.add(n // divisors)  # get upper divisor from lower [ 16 / 1 = 16, 16 / 2 = 8, 16 / 4 = 4]
        return factors  # [1, 2, 4, 8 16]


    print(generate_factors(12)) # {1, 2, 3, 4, 6, 12} -> pycharm output

 Pierre Vriens hopefully this makes more sense. this is an O(nlogn) solution. 
 import math

    '''
    I applied finding prime factorization to solve this. (Trial Division)
    It's not complicated
    '''


    def generate_factors(n):
        lower_bound_check = int(math.sqrt(n))  # determine lowest bound divisor range [16 = 4]
        factors = set()  # store factors
        for divisors in range(1, lower_bound_check + 1):  # loop [1 .. 4]
            if n % divisors == 0:
                factors.add(divisors)  # lower bound divisor is found 16 [ 1, 2, 4]
                factors.add(n // divisors)  # get upper divisor from lower [ 16 / 1 = 16, 16 / 2 = 8, 16 / 4 = 4]
        return factors  # [1, 2, 4, 8 16]


    print(generate_factors(12)) # {1, 2, 3, 4, 6, 12} -> pycharm output

 Pierre Vriens hopefully this makes more sense. this is an O(nlogn) solution. 

回答 17

使用与以下列表推导一样简单的方法,请注意,我们不需要测试1和我们要查找的数字:

def factors(n):
    return [x for x in range(2, n//2+1) if n%x == 0]

关于平方根的使用,假设我们要查找10的因数。sqrt(10) = 4因此range(1, int(sqrt(10))) = [1, 2, 3, 4],求4 的整数部分显然未命中5。

除非我想念什么,否则我建议您使用,如果您必须这样做int(ceil(sqrt(x)))。当然,这会产生很多不必要的函数调用。

Use something as simple as the following list comprehension, noting that we do not need to test 1 and the number we are trying to find:

def factors(n):
    return [x for x in range(2, n//2+1) if n%x == 0]

In reference to the use of square root, say we want to find factors of 10. The integer portion of the sqrt(10) = 4 therefore range(1, int(sqrt(10))) = [1, 2, 3, 4] and testing up to 4 clearly misses 5.

Unless I am missing something I would suggest, if you must do it this way, using int(ceil(sqrt(x))). Of course this produces a lot of unnecessary calls to functions.


回答 18

我认为为了提高可读性和速度,@ oxrock的解决方案是最好的,所以这里是为python 3+重写的代码:

def num_factors(n):
    results = set()
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0: results.update([i,int(n/i)])
    return results

I think for readability and speed @oxrock’s solution is the best, so here is the code rewritten for python 3+:

def num_factors(n):
    results = set()
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0: results.update([i,int(n/i)])
    return results

回答 19

当我看到这个问题,即使numpy 比python循环快得多的时候,也没有人使用numpy,我感到非常惊讶。通过使用numpy实现@agf的解决方案,结果平均8倍。我相信,如果您以numpy实施其他一些解决方案,您将获得美好的时光。

这是我的功能:

import numpy as np
def b(n):
    r = np.arange(1, int(n ** 0.5) + 1)
    x = r[np.mod(n, r) == 0]
    return set(np.concatenate((x, n / x), axis=None))   

请注意,x轴的数字不是功能的输入。函数的输入为2,x轴上的数字为负1。因此,输入十是2 ** 10-1 = 1023

I was pretty surprised when I saw this question that no one used numpy even when numpy is way faster than python loops. By implementing @agf’s solution with numpy and it turned out at average 8x faster. I belive that if you implemented some of the other solutions in numpy you could get amazing times.

Here is my function:

import numpy as np
def b(n):
    r = np.arange(1, int(n ** 0.5) + 1)
    x = r[np.mod(n, r) == 0]
    return set(np.concatenate((x, n / x), axis=None))   

Notice that the numbers of the x-axis are not the input to the functions. The input to the functions is 2 to the the number on the x-axis minus 1. So where ten is the input would be 2**10-1 = 1023


回答 20

import 'dart:math';
generateFactorsOfN(N){
  //determine lowest bound divisor range
  final lowerBoundCheck = sqrt(N).toInt();
  var factors = Set<int>(); //stores factors
  /**
   * Lets take 16:
   * 4 = sqrt(16)
   * start from 1 ...  4 inclusive
   * check mod 16 % 1 == 0?  set[1, (16 / 1)]
   * check mod 16 % 2 == 0?  set[1, (16 / 1) , 2 , (16 / 2)]
   * check mod 16 % 3 == 0?  set[1, (16 / 1) , 2 , (16 / 2)] -> unchanged
   * check mod 16 % 4 == 0?  set[1, (16 / 1) , 2 , (16 / 2), 4, (16 / 4)]
   *
   *  ******************* set is used to remove duplicate
   *  ******************* case 4 and (16 / 4) both equal to 4
   *  return factor set<int>.. this isn't ordered
   */

  for(var divisor = 1; divisor <= lowerBoundCheck; divisor++){
    if(N % divisor == 0){
      factors.add(divisor);
      factors.add(N ~/ divisor); // ~/ integer division 
    }
  }
  return factors;
}
import 'dart:math';
generateFactorsOfN(N){
  //determine lowest bound divisor range
  final lowerBoundCheck = sqrt(N).toInt();
  var factors = Set<int>(); //stores factors
  /**
   * Lets take 16:
   * 4 = sqrt(16)
   * start from 1 ...  4 inclusive
   * check mod 16 % 1 == 0?  set[1, (16 / 1)]
   * check mod 16 % 2 == 0?  set[1, (16 / 1) , 2 , (16 / 2)]
   * check mod 16 % 3 == 0?  set[1, (16 / 1) , 2 , (16 / 2)] -> unchanged
   * check mod 16 % 4 == 0?  set[1, (16 / 1) , 2 , (16 / 2), 4, (16 / 4)]
   *
   *  ******************* set is used to remove duplicate
   *  ******************* case 4 and (16 / 4) both equal to 4
   *  return factor set<int>.. this isn't ordered
   */

  for(var divisor = 1; divisor <= lowerBoundCheck; divisor++){
    if(N % divisor == 0){
      factors.add(divisor);
      factors.add(N ~/ divisor); // ~/ integer division 
    }
  }
  return factors;
}

回答 21

我认为这是最简单的方法:

    x = 23

    i = 1
    while i <= x:
      if x % i == 0:
        print("factor: %s"% i)
      i += 1

I reckon this is the simplest way to do that:

    x = 23

    i = 1
    while i <= x:
      if x % i == 0:
        print("factor: %s"% i)
      i += 1

如何在Windows上安装PyCrypto?

问题:如何在Windows上安装PyCrypto?

我已经阅读了所有其他Google来源和SO线程,但没有任何效果。

Python 2.7.3 32bit安装在上Windows 7 64bit。下载,解压缩然后尝试在以下位置安装PyCrypto结果"Unable to find vcvarsall.bat".

因此,我安装了MinGW,并将其作为选择的编译器安装在安装线上。但是然后我得到了错误"RuntimeError: chmod error".

我该如何解决这个问题?我试过使用pip,它给出相同的结果。我找到了一个预构建的PyCrypto 2.3二进制文件并进行了安装,但是在系统上找不到该文件(无法正常工作)。

有任何想法吗?

I’ve read every other google source and SO thread, with nothing working.

Python 2.7.3 32bit installed on Windows 7 64bit. Download, extracting, and then trying to install PyCrypto results in "Unable to find vcvarsall.bat".

So I install MinGW and tack that on the install line as the compiler of choice. But then I get the error "RuntimeError: chmod error".

How in the world do I get around this? I’ve tried using pip, which gives the same result. I found a prebuilt PyCrypto 2.3 binary and installed that, but it’s nowhere to be found on the system (not working).

Any ideas?


回答 0

如果尚未安装与Python.org分发的Visual Studio二进制文件兼容的C / C ++开发环境,则应坚持仅安装纯Python软件包或可用于Windows二进制文件的软件包。

幸运的是,有适用于Windows的PyCrypto二进制文件:http ://www.voidspace.org.uk/python/modules.shtml#pycrypto

更新:
正如@Udi在下面的注释中建议的那样,以下命令也将安装pycrypto并可以在其中使用virtualenv

easy_install http://www.voidspace.org.uk/python/pycrypto-2.6.1/pycrypto-2.6.1.win32-py2.7.exe

注意,请从此列表中选择与您的设置相关的链接

如果您正在寻找适用于Python 3.5的版本,请参阅python 3.5上的PyCrypto

If you don’t already have a C/C++ development environment installed that is compatible with the Visual Studio binaries distributed by Python.org, then you should stick to installing only pure Python packages or packages for which a Windows binary is available.

Fortunately, there are PyCrypto binaries available for Windows: http://www.voidspace.org.uk/python/modules.shtml#pycrypto

UPDATE:
As @Udi suggests in the comment below, the following command also installs pycrypto and can be used in virtualenv as well:

easy_install http://www.voidspace.org.uk/python/pycrypto-2.6.1/pycrypto-2.6.1.win32-py2.7.exe

Notice to choose the relevant link for your setup from this list

If you’re looking for builds for Python 3.5, see PyCrypto on python 3.5


回答 1

Microsoft最近最近发布了用于Python 2.7的独立的专用Microsoft Visual C ++编译器。如果您使用的是Python 2.7,只需安装该编译器和Setuptools 6.0或更高版本,大多数具有C扩展名的软件包现在都可以轻松编译。

Microsoft has recently recently released a standalone, dedicated Microsoft Visual C++ Compiler for Python 2.7. If you’re using Python 2.7, simply install that compiler and Setuptools 6.0 or later, and most packages with C extensions will now compile readily.


回答 2

经过数年之久,python终于同意了一种二进制发行版wheel,该发行版允许在Windows上安装甚至二进制扩展,而无需使用简单的编译器pip install packagename。有一种流行的软件包列表及其状态。Pycrypto还不存在,例如lxml,PySide和Scrapy。

2015年11月编辑pip uninstall pycryptopip install pycryptodome。这是pycrypto具有新功能的前叉,并且支持车轮。它取代了pycrypto,因此现有代码将继续起作用(请参阅https://pycryptodome.readthedocs.org/en/latest/src/examples.html

After years and years, python finally agreed for a binary disribution called wheel which allows to install even binary extensions on Windows without having a compiler with simple pip install packagename. There is a list of popular packages with their status. Pycrypto is not there yet, but lxml, PySide and Scrapy for example.

Edited Nov 2015: pip uninstall pycrypto & pip install pycryptodome. It is a pycrypto fork with new features and it supports wheel. It replaces pycrypto, so existing code will continue to work (see https://pycryptodome.readthedocs.org/en/latest/src/examples.html)


回答 3

对于VS2010:

SET VS90COMNTOOLS=%VS100COMNTOOLS%

对于VS2012:

SET VS90COMNTOOLS=%VS110COMNTOOLS%

然后调用:

pip install pyCrypto 

For VS2010:

SET VS90COMNTOOLS=%VS100COMNTOOLS%

For VS2012:

SET VS90COMNTOOLS=%VS110COMNTOOLS%

then Call:

pip install pyCrypto 

回答 4

一般来说

vcvarsall.bat是Visual C ++编译器的一部分,您需要安装要安装的内容。如果您的Python是使用Visual Studio工具链编译的,甚至不要尝试处理MingGW,反之亦然。甚至Microsoft工具链的版本也很重要。用VS 2008编译的Python不能与用VS 2010编译的扩展一起使用!

您必须使用与Python版本相同的编译器来编译PyCrypto。Google的“无法找到vcvarsall.bat”,因为这是问题的根源,这是在Windows上编译Python扩展的一个非常普遍的问题。

在使用此链接的任何系统上,要获得正确的信息,都需要大量的信息和大量的阅读。

注意使用Visual Studio 2010或不使用Visual Studio 2008

据我所知,以下仍然是正确的。这是在2010年6月的链接中发布的,该链接是指尝试针对python.org上可用的Python安装程序使用VS 2010 Express构建扩展。

如果这样做,请小心。python.org的Python 2.6和2.7是使用Visual Studio 2008编译器构建的。您将需要使用与Python相同的CRT(msvcr90.dll)进行链接。

Visual Studio 2010 Express链接到错误的CRT版本:msvcr100.dll。

如果执行此操作,则还必须使用Visual Studio 2010 Express重新构建Python。您不能将标准的Python二进制安装程序用于Windows。您也不能使用使用与Visual Studio 2010(Express)不同的编译器构建的任何C / C ++扩展。

意见:这是我放弃Windows从事OSX的所有重要开发工作的原因之一!

In general

vcvarsall.bat is part of the Visual C++ compiler, you need that to install what you are trying to install. Don’t even try to deal with MingGW if your Python was compiled with Visual Studio toolchain and vice versa. Even the version of the Microsoft tool chain is important. Python compiled with VS 2008 won’t work with extensions compiled with VS 2010!

You have to compile PyCrypto with the same compiler that the version of Python was compiled with. Google for “Unable to find vcvarsall.bat” because that is the root of your problem, it is a very common problem with compiling Python extensions on Windows.

There is a lot of information and a lot to read to get this right on whatever system you are on with this link.

Beware using Visual Studio 2010 or not using Visual Studio 2008

As far as I know the following is still true. This was posted in the link above in June, 2010 referring to trying to build extensions with VS 2010 Express against the Python installers available on python.org.

Be careful if you do this. Python 2.6 and 2.7 from python.org are built with Visual Studio 2008 compilers. You will need to link with the same CRT (msvcr90.dll) as Python.

Visual Studio 2010 Express links with the wrong CRT version: msvcr100.dll.

If you do this, you must also re-build Python with Visual Studio 2010 Express. You cannot use the standard Python binary installer for Windows. Nor can you use any C/C++ extensions built with a different compiler than Visual Studio 2010 (Express).

Opinion: This is one reason I abandoned Windows for all serious development work for OSX!


回答 5

PyCryptodomePyCrypto的几乎兼容的分支,带有pypi上的Windows轮子。

您可以使用以下简单的方法安装它:

pip install pycryptodome

该网站还包含使用Microsoft编译器从源进行构建的说明。

PyCryptodome is an almost-compatible fork of PyCrypto with Windows wheels available on pypi.

You can install it with a simple:

pip install pycryptodome

The website includes instructions to build it from sources with the Microsoft compilers too.


回答 6

我设法pycrypto通过使用MinGW32和进行编译MSYS。假定您已安装pipeasy_install安装。

这是我的做法:

1)安装MinGW32。为了便于说明,我们假设它已安装在中C:\MinGW。我建议使用安装程序时,选择. MSYS应该随其一起安装的C ++编译器MinGW

2)添加c:\mingw\bin,c:\mingw\mingw32\bin,C:\MinGW\msys\1.0, c:\mingw\msys\1.0\bin and c:\mingw\msys\1.0\sbin到您的中%PATH%。如果您不熟悉,这篇文章会很有帮助。

3)从搜索栏中运行msys,然后MSYS终端将打开。对于熟悉的人Cygwin,它的工作方式类似。

4)pip install pycrypto在此之后,应从MSYS终端内部正常运行。

I have managed to get pycrypto to compile by using MinGW32 and MSYS. This presumes that you have pip or easy_install installed.

Here’s how I did it:

1) Install MinGW32. For the sake of this explanation, let’s assume it’s installed in C:\MinGW. When using the installer, which I recommend, select the C++ compiler. MSYS should install with MinGW

2) Add c:\mingw\bin,c:\mingw\mingw32\bin,C:\MinGW\msys\1.0, c:\mingw\msys\1.0\bin and c:\mingw\msys\1.0\sbin to your %PATH%. If you aren’t familiar, this article is very helpful.

3) From the search bar, run msys and the MSYS terminal will open. For those familiar with Cygwin, it works in a similar fashion.

4) From within the MSYS terminal pip install pycrypto should run without error after this.


回答 7

对于Windows 7:

要在Windows中安装Pycrypto,

在命令提示符中尝试此操作,

设置路径= C:\ Python27 \ Scripts(即easy_install所在的路径)

然后执行以下命令

easy_install pycrypto

对于Ubuntu:

试试这个,

从“ https://pypi.python.org/pypi/pycrypto ” 下载Pycrypto

然后使用终端将当前路径更改为下载路径,并且用户应为root:

例如:root @ xyz-virtual-machine:〜/ pycrypto-2.6.1#

然后使用终端执行以下命令:

python setup.py安装

对我有用。希望为所有人服务。

For Windows 7:

To install Pycrypto in Windows,

Try this in Command Prompt,

Set path=C:\Python27\Scripts (i.e path where easy_install is located)

Then execute the following,

easy_install pycrypto

For Ubuntu:

Try this,

Download Pycrypto from “https://pypi.python.org/pypi/pycrypto

Then change your current path to downloaded path using your terminal and user should be root:

Eg: root@xyz-virtual-machine:~/pycrypto-2.6.1#

Then execute the following using the terminal:

python setup.py install

It’s worked for me. Hope works for all..


回答 8

对于那些正在寻找python 3.4的人,我发现了一个可以正常使用的安装程序的git repo。这是x64的直接链接x32

For those of you looking for python 3.4 I found a git repo with an installer that just works. Here are the direct links for x64 and x32


回答 9

可以使用Windows 7 SDK工具包构建PyCrypto。Windows 7 SDK有两个版本。原始版本(针对.Net 3.5)包括VS 2008命令行编译器。可以同时安装32位和64位编译器。

第一步是编译mpir以提供快速算法。我已经记录了在gmpy库中使用的过程。可以在sdk_build中找到使用SDK编译器构建mpir的详细说明。

在DOS提示符下使用SDK编译器的关键步骤是:

1)根据需要运行vcvars32.bat或vcvars64.bat。

2)在提示符下,执行“ set MSSdk = 1”

3)在提示符下,执行“ set DISTUTILS_USE_SDK = 1”

假设C代码没有其他问题,这应该可以使“ python setup.py install”成功。但是我模糊地记得我不得不编辑几个PyCrypto文件来启用mpir并找到mpir库,但是目前我没有Windows系统。我将需要几天的时间来重新创建这些步骤。如果您到那时还没有报告成功,我将发布PyCrypto步骤。这些步骤将假定您能够编译mpir。

我希望这有帮助。

It’s possible to build PyCrypto using the Windows 7 SDK toolkits. There are two versions of the Windows 7 SDK. The original version (for .Net 3.5) includes the VS 2008 command-line compilers. Both 32- and 64-bit compilers can be installed.

The first step is to compile mpir to provide fast arithmetic. I’ve documented the process I use in the gmpy library. Detailed instructions for building mpir using the SDK compiler can be found at sdk_build

The key steps to use the SDK compilers from a DOS prompt are:

1) Run either vcvars32.bat or vcvars64.bat as appropriate.

2) At the prompt, execute “set MSSdk=1”

3) At the prompt, execute “set DISTUTILS_USE_SDK=1”

This should allow “python setup.py install” to succeed assuming there are no other issues with the C code. But I vaaguely remember that I had to edit a couple of PyCrypto files to enable mpir and to find the mpir libraries but I don’t have my Windows system up at the moment. It will be a couple of days before I’ll have time to recreate the steps. If you haven’t reported success by then, I’ll post the PyCrypto steps. The steps will assume you were able to compile mpir.

I hope this helps.


回答 10

尝试仅使用:

pip install pycryptodome

要么:

pip install pycryptodomex

来源:https : //pypi.python.org/pypi/pycryptodome

Try just using:

pip install pycryptodome

or:

pip install pycryptodomex

Source: https://pypi.python.org/pypi/pycryptodome


回答 11

因此,我安装了MinGW,并将其作为选择的编译器安装在安装线上。但是,然后我得到错误“ RuntimeError:chmod错误”。

"RuntimeError: chmod error"发生此错误是因为安装脚本找不到chmod命令。

我该如何解决这个问题?

您只需要将MSYS二进制文件添加到PATH并重新运行安装脚本即可

(注意:MinGW随MSYS一起提供)

例如,如果我们在文件夹中 C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1>

C:\.....>set PATH=C:\MinGW\msys\1.0\bin;%PATH%
C:\.....>python setup.py install

可选:在重新运行脚本之前,您可能需要清理:

`C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1> python setup.py clean`

So I install MinGW and tack that on the install line as the compiler of choice. But then I get the error “RuntimeError: chmod error”.

This error "RuntimeError: chmod error" occurs because the install script didn’t find the chmod command.

How in the world do I get around this?

Solution

You only need to add the MSYS binaries to the PATH and re-run the install script.

(N.B: Note that MinGW comes with MSYS so )

Example

For example, if we are in folder C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1>

C:\.....>set PATH=C:\MinGW\msys\1.0\bin;%PATH%
C:\.....>python setup.py install

Optional: you might need to clean before you re-run the script:

`C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1> python setup.py clean`

回答 12

  1. 转到适用于Python 2.7的Microsoft Visual C ++编译器 ”,然后根据“系统要求”继续操作(这是我按照以下步骤进行的工作)。

  2. 安装setuptools(6.0或更高版本需要Python来自动检测setuptools的这个编译器包) 或者通过: pip install setuptools 下载“setuptools的自举安装”源来自于你的filestystem为“ez_python.py”,保存这个文件somwhere与安装:python ez_python.py

  3. 安装滚轮(建议使用滚轮来生产预构建的二进制软件包)。您可以使用以下方法安装它:pip install wheel

  4. 打开Windows提升的命令提示符cmd.exe(带有“以管理员身份运行”),为所有用户安装“适用于Python 2.7的Microsoft Visual C ++编译器”。您可以使用以下命令来执行此操作:msiexec / i C:\users\jozko\download\VCForPython27.msi ALLUSERS=1仅使用您自己的文件路径:msiexec /i <path to MSI> ALLUSERS=1

  5. 现在,您应该能够使用以下命令安装pycrypto: pip install pycrypto

  1. Go to Microsoft Visual C++ Compiler for Python 2.7 and continue based on “System Requirements” (this is what I did to put below steps together).

  2. Install setuptools (setuptools 6.0 or later is required for Python to automatically detect this compiler package) either by: pip install setuptools or download “Setuptools bootstrapping installer” source from, save this file somwhere on your filestystem as “ez_python.py” and install with: python ez_python.py

  3. Install wheel (wheel is recommended for producing pre-built binary packages). You can install it with: pip install wheel

  4. Open Windows elevated Command Prompt cmd.exe (with “Run as administrator”) to install “Microsoft Visual C++ Compiler for Python 2.7” for all users. You can use following command to do so: msiexec /i C:\users\jozko\download\VCForPython27.msi ALLUSERS=1 just use your own path to file: msiexec /i <path to MSI> ALLUSERS=1

  5. Now you should be able to install pycrypto with: pip install pycrypto


回答 13

如果您在Windows上并且正在努力安装Pycrypcto,请使用:pip install pycryptodome。它像奇迹一样运作,比起进行大量的配置和调整将使您的生活更加轻松。

If you are on Windows and struggling with installing Pycrypcto just use the: pip install pycryptodome. It works like a miracle and it will make your life much easier than trying to do a lot of configurations and tweaks.


回答 14

这可能不是最佳解决方案,但是您可以从MS下载并安装免费的Visual C ++ Express软件包。这将为您提供编译PyCrypto代码所需的C ++编译器。

This probably isn’t the optimal solution but you might download and install the free Visual C++ Express package from MS. This will give you the C++ compiler you need to compile the PyCrypto code.


回答 15

我的答案可能与此处提到的问题无关,但是我对Python 3.4遇到了同样的问题,其中Crypto.Cipher不是有效的导入。所以我尝试安装PyCrypto并遇到问题。

经过研究,我发现3.4应该使用pycryptodome

我使用pycharm安装pycryptodome,效果很好。

Crypto.Cipher导入AES

My answer might not be related to problem mention here, but I had same problem with Python 3.4 where Crypto.Cipher wasn’t a valid import. So I tried installing PyCrypto and went into problems.

After some research I found with 3.4 you should use pycryptodome.

I install pycryptodome using pycharm and I was good.

from Crypto.Cipher import AES


回答 16

因此,我安装了MinGW,并将其作为选择的编译器安装在安装线上。但是,然后我得到错误“ RuntimeError:chmod错误”。

您需要在MinGW下安装msys软件包

并在PATH env变量中添加以下条目。

  • C:\MinGW\bin
  • C:\MinGW\msys\1.0\bin [在这里您将找到chmod可执行文件]

然后从正常的Windows命令提示符下运行命令。

So I install MinGW and tack that on the install line as the compiler of choice. But then I get the error “RuntimeError: chmod error”.

You need to install msys package under MinGW

and add following entries in your PATH env variable.

  • C:\MinGW\bin
  • C:\MinGW\msys\1.0\bin [This is where you will find chmod executable]

Then run your command from normal windows command prompt.


回答 17

由于奇怪的法律原因,二进制文件无法正常发布。空隙空间通常是最好的第二个来源。但是由于相当长的一段时间,voidspace维护程序没有更新。使用[ https://www.dropbox.com/s/n6rckn0k6u4nqke/pycrypto-2.6.1.zip?dl=0]中的压缩文件

Due to weird legal reasons, binaries are not published the normal way. Voidspace is normally the best second source. But since quite some time, voidspace maintainer did not update. Use the zip from [https://www.dropbox.com/s/n6rckn0k6u4nqke/pycrypto-2.6.1.zip?dl=0]


回答 18

步骤1:从此处安装Visual C ++ 2010 Express 。

(请勿安装Microsoft Visual Studio 2010 Service Pack 1)

步骤2:从“控制面板\程序和功能”中删除所有Microsoft Visual C ++ 2010可再发行程序包。如果您不这样做,则安装将失败,并显示模糊的“安装过程中出现致命错误”错误。

步骤3:从此处安装用于Visual Studio 2010(v7.1)的Windows SDK的脱机版本。这是64位扩展所必需的。Windows内置了对Pismo等ISO的安装。

步骤4:您需要使用Pismo File Mount Audit Package安装ISO文件。从这里下载Pismo

步骤5:右键单击下载的ISO文件,然后选择Pismo挂载。之后,安装Setup \ SDKSetup.exe而不是setup.exe。

步骤6a:通过将目录更改为C:\ Program Files(x86)\ Microsoft Visual Studio版本\ VC \在C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC \ bin \ amd64中创建vcvars64.bat文件命令提示符。在命令提示符下键入命令: cd C:\Program Files (x86)\Microsoft Visual Studio version\VC\r

步骤6b:要为针对x86平台的64位命令行构建配置此“命令提示符”窗口,请在命令提示符下输入: vcvarsall x86单击此处以获取更多选项。

步骤7:在命令提示符下,输入以下命令安装PyCrypto: C:\Python3X>pip install -U your_wh_file

Step 1: Install Visual C++ 2010 Express from here.

(Do not install Microsoft Visual Studio 2010 Service Pack 1 )

Step 2: Remove all the Microsoft Visual C++ 2010 Redistributable packages from Control Panel\Programs and Features. If you don’t do those then the install is going to fail with an obscure “Fatal error during installation” error.

Step 3: Install offline version of Windows SDK for Visual Studio 2010 (v7.1) from here. This is required for 64bit extensions. Windows has builtin mounting for ISOs like Pismo.

Step 4: You need to install the ISO file with Pismo File Mount Audit Package. Download Pismo from here

Step 5: Right click the downloaded ISO file and choose mount with Pismo. Thereafter, install the Setup\SDKSetup.exe instead of setup.exe.

Step 6a: Create a vcvars64.bat file in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64 by changing directory to C:\Program Files (x86)\Microsoft Visual Studio version\VC\ on the command prompt. Type command on the command prompt: cd C:\Program Files (x86)\Microsoft Visual Studio version\VC\r

Step 6b: To configure this Command Prompt window for 64-bit command-line builds that target x86 platforms, at the command prompt, enter: vcvarsall x86 Click here for more options.

Step 7: At the command prompt, install the PyCrypto by typing: C:\Python3X>pip install -U your_wh_file


回答 19

我有python的Pycharm。

  1. pycharm -> file -> setting -> project interpreter

  2. 点击 +

  3. 搜索"pycrypto"并安装软件包

注意:如果尚未安装“适用于Python 2.7的Microsoft Visual C ++编译器”,则它将提示安装,一旦安装完成,请尝试上述步骤,即可正常工作。

I had Pycharm for python.

  1. Go to pycharm -> file -> setting -> project interpreter

  2. Click on +

  3. Search for "pycrypto" and install the package

Note: If you don’t have “Microsoft Visual C++ Compiler for Python 2.7” installed then it will prompt for installation, once installation finished try the above steps it should work fine.


bs4.FeatureNotFound:找不到具有您请求的功能的树构建器:lxml。您需要安装解析器库吗?

问题:bs4.FeatureNotFound:找不到具有您请求的功能的树构建器:lxml。您需要安装解析器库吗?

...
soup = BeautifulSoup(html, "lxml")
File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 152, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

以上输出在我的终端上。我在Mac OS 10.7.x上。我有Python 2.7.1,并按照本教程操作获得了Beautiful Soup和lxml,它们都已成功安装并与位于此处的单独测试文件一起使用。在导致此错误的Python脚本中,我包含以下行: from pageCrawler import comparePages 在pageCrawler文件中,我包含以下两行: from bs4 import BeautifulSoup from urllib2 import urlopen

找出问题所在以及如何解决的任何帮助将不胜感激。

...
soup = BeautifulSoup(html, "lxml")
File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 152, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

The above outputs on my Terminal. I am on Mac OS 10.7.x. I have Python 2.7.1, and followed this tutorial to get Beautiful Soup and lxml, which both installed successfully and work with a separate test file located here. In the Python script that causes this error, I have included this line: from pageCrawler import comparePages And in the pageCrawler file I have included the following two lines: from bs4 import BeautifulSoup from urllib2 import urlopen

Any help in figuring out what the problem is and how it can be solved would much be appreciated.


回答 0

我怀疑这与BS将用于读取HTML的解析器有关。他们的文档在这里,但是如果您像我(在OSX上)一样,可能会遇到一些麻烦,需要做一些工作:

您会注意到,在上面的BS4文档页面中,他们指出,默认情况下,BS4将使用Python内置的HTML解析器。假设您使用的是OSX,则Apple捆绑的Python版本是2.7.2,它对字符格式不宽容。我遇到了同样的问题,因此我升级了Python版本以解决此问题。在virtualenv中执行此操作可以最大程度地减少对其他项目的破坏。

如果这样做听起来很痛苦,则可以切换到LXML解析器:

pip install lxml

然后尝试:

soup = BeautifulSoup(html, "lxml")

根据您的情况,这可能就足够了。我发现这很烦人,需要升级我的Python版本。使用的virtualenv,您可以迁移的包很容易。

I have a suspicion that this is related to the parser that BS will use to read the HTML. They document is here, but if you’re like me (on OSX) you might be stuck with something that requires a bit of work:

You’ll notice that in the BS4 documentation page above, they point out that by default BS4 will use the Python built-in HTML parser. Assuming you are in OSX, the Apple-bundled version of Python is 2.7.2 which is not lenient for character formatting. I hit this same problem, so I upgraded my version of Python to work around it. Doing this in a virtualenv will minimize disruption to other projects.

If doing that sounds like a pain, you can switch over to the LXML parser:

pip install lxml

And then try:

soup = BeautifulSoup(html, "lxml")

Depending on your scenario, that might be good enough. I found this annoying enough to warrant upgrading my version of Python. Using virtualenv, you can migrate your packages fairly easily.


回答 1

对于安装了bs4的基本开箱即用的python,则可以使用以下命令处理xml

soup = BeautifulSoup(html, "html5lib")

但是,如果您想使用formatter =’xml’,则需要

pip3 install lxml

soup = BeautifulSoup(html, features="xml")

For basic out of the box python with bs4 installed then you can process your xml with

soup = BeautifulSoup(html, "html5lib")

If however you want to use formatter=’xml’ then you need to

pip3 install lxml

soup = BeautifulSoup(html, features="xml")

回答 2

我首选内置python html解析器,不安装不依赖

soup = BeautifulSoup(s, "html.parser")

I preferred built in python html parser, no install no dependencies

soup = BeautifulSoup(s, "html.parser")


回答 3

我正在使用Python 3.6,并且在这篇文章中有相同的原始错误。运行命令后:

python3 -m pip install lxml

它解决了我的问题

I am using Python 3.6 and I had the same original error in this post. After I ran the command:

python3 -m pip install lxml

it resolved my problem


回答 4

运行以下三个命令,以确保已安装所有相关的软件包:

pip install bs4
pip install html5lib
pip install lxml

然后根据需要重新启动Python IDE。

那应该处理与这个问题有关的任何事情。

Run these three commands to make sure that you have all the relevant packages installed:

pip install bs4
pip install html5lib
pip install lxml

Then restart your Python IDE, if needed.

That should take care of anything related to this issue.


回答 5

您可以使用以下代码代替使用lxml和html.parser:

soup = BeautifulSoup(html, 'html.parser')

Instead of using lxml use html.parser, you can use this piece of code:

soup = BeautifulSoup(html, 'html.parser')

回答 6

尽管BeautifulSoup默认情况下支持HTML解析器。但是,如果您想使用任何其他第三方Python解析器,则需要安装该外部解析器,例如(lxml)。

soup_object= BeautifulSoup(markup,"html.parser") #Python HTML parser

但是,如果您未将任何解析器指定为参数,则会收到一条警告,提示您未指定解析器。

soup_object= BeautifulSoup(markup) #Warnning

要使用任何其他外部解析器,您需要先安装它,然后再指定它。喜欢

pip install lxml

soup_object= BeautifulSoup(markup,'lxml') # C dependent parser 

外部解析器具有c和python依赖关系,这可能有一些优点和缺点。

Although BeautifulSoup supports the HTML parser by default If you want to use any other third-party Python parsers you need to install that external parser like(lxml).

soup_object= BeautifulSoup(markup,"html.parser") #Python HTML parser

But if you don’t specified any parser as parameter you will get an warning that no parser specified.

soup_object= BeautifulSoup(markup) #Warnning

To use any other external parser you need to install it and then need to specify it. like

pip install lxml

soup_object= BeautifulSoup(markup,'lxml') # C dependent parser 

External parser have c and python dependency which may have some advantage and disadvantage.


回答 7

我遇到了同样的问题。我发现原因是我有一个过时的python 6软件包。

>>> import html5lib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/html5lib/__init__.py", line 16, in <module>
    from .html5parser import HTMLParser, parse, parseFragment
  File "/usr/local/lib/python2.7/site-packages/html5lib/html5parser.py", line 2, in <module>
    from six import with_metaclass, viewkeys, PY3
ImportError: cannot import name viewkeys

升级六个软件包将解决此问题:

sudo pip install six=1.10.0

I encountered the same issue. I found the reason is that I had a slightly-outdated python six package.

>>> import html5lib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/html5lib/__init__.py", line 16, in <module>
    from .html5parser import HTMLParser, parse, parseFragment
  File "/usr/local/lib/python2.7/site-packages/html5lib/html5parser.py", line 2, in <module>
    from six import with_metaclass, viewkeys, PY3
ImportError: cannot import name viewkeys

Upgrading your six package will solve the issue:

sudo pip install six=1.10.0

回答 8

在python环境中安装LXML分析器。

pip install lxml

您的问题将得到解决。您还可以使用内置的python软件包,其用法与以下相同:

soup = BeautifulSoup(s,  "html.parser")

注意:在Python3中,“ HTMLParser”模块已重命名为“ html.parser”

Install LXML parser in python environment.

pip install lxml

Your problem will be resolve. You can also use built-in python package for the same as:

soup = BeautifulSoup(s,  "html.parser")

Note: The “HTMLParser” module has been renamed to “html.parser” in Python3


回答 9

在某些参考中,使用第二个而不是第一个:

soup_object= BeautifulSoup(markup,'html-parser')
soup_object= BeautifulSoup(markup,'html.parser')

In some references, use the second instead of the first:

soup_object= BeautifulSoup(markup,'html-parser')
soup_object= BeautifulSoup(markup,'html.parser')

回答 10

由于使用了解析器,因此出现错误。通常,如果您具有HTML文件/代码,则需要使用html5lib(文档可在此处找到);如果您具有XML文件/数据,则需要使用lxml(文档可在此处找到)。您也可以将其lxml用于HTML文件/代码,但有时会出现上述错误。因此,最好根据数据/文件的类型明智地选择软件包。您也可以使用html_parser内置模块。但是,这有时有时也不起作用。

有关何时使用哪个软件包的更多详细信息,请参见此处的详细信息

The error is coming because of the parser you are using. In general, if you have HTML file/code then you need to use html5lib(documentation can be found here) & in-case you have XML file/data then you need to use lxml(documentation can be found here). You can use lxml for HTML file/code also but sometimes it gives an error as above. So, better to choose the package wisely based on the type of data/file. You can also use html_parser which is built-in module. But, this also sometimes do not work.

For more details regarding when to use which package you can see the details here


回答 11

空白参数将导致警告,提示您最好使用该参数。
汤= BeautifulSoup(html)

————— // UserWarning:未明确指定解析器,因此我正在为此系统使用最佳的HTML解析器(“ html5lib”)。通常这不是问题,但是如果您在另一个系统或不同的虚拟环境中运行此代码,则它可能使用不同的解析器并且行为不同。 ——- /

python –version Python 3.7.7

PyCharm 19.3.4 CE

Blank parameter will result in a warning for best available.
soup = BeautifulSoup(html)

—————/UserWarning: No parser was explicitly specified, so I’m using the best available HTML parser for this system (“html5lib”). This usually isn’t a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.———————-/

python –version Python 3.7.7

PyCharm 19.3.4 CE


input()错误-NameError:名称“ …”未定义

问题:input()错误-NameError:名称“ …”未定义

尝试运行此简单脚本时出现错误:

input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

假设我输入“花花公子”,我得到的错误是:

line 1, in <module>
input_variable = input ("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'dude' is not defined

我正在使用python 2.7运行这些脚本。

I am getting an error when I try to run this simple script:

input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

Let’s say I type in “dude”, the error I am getting is:

line 1, in <module>
input_variable = input ("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'dude' is not defined

I am running these scripts with Python 2.7.


回答 0

TL; DR

inputPython 2.7中的函数,将您输入的内容评估为Python表达式。如果您只是想读取字符串,请使用raw_inputPython 2.7中的函数,该函数不会评估读取的字符串。

如果您使用的是Python 3.x,raw_input则已重命名为input。引用Python 3.0发行说明

raw_input()已重命名为input()。也就是说,新input()函数从中读取一行,sys.stdin并删除尾随的换行符来返回它。EOFError如果输入过早终止,它将触发。要获取的旧行为input(),请使用eval(input())


在Python 2.7中,有两个函数可用于接受用户输入。一个是input,另一个是raw_input。您可以想到它们之间的关系如下

input = eval(raw_input)

考虑下面的代码以更好地理解这一点

>>> dude = "thefourtheye"
>>> input_variable = input("Enter your name: ")
Enter your name: dude
>>> input_variable
'thefourtheye'

input从用户接受一个字符串,并在当前Python上下文中评估该字符串。当我输入dude作为输入时,它将发现dude绑定到该值thefourtheye,因此求值结果变为,thefourtheye并将其分配给input_variable

如果我输入当前python上下文中不存在的其他内容,它将失败NameError

>>> input("Enter your name: ")
Enter your name: dummy
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'dummy' is not defined

Python 2.7的安全注意事项input

由于评估了任何用户类型,因此也存在安全问题。例如,如果您已经使用加载os了程序中的模块import os,然后用户输入

os.remove("/etc/hosts")

它将由python评估为函数调用表达式,并将执行该函数。如果您以提升的权限执行Python,则/etc/hosts文件将被删除。瞧,这有多危险?

为了演示这一点,让我们尝试input再次执行函数。

>>> dude = "thefourtheye"
>>> input("Enter your name: ")
Enter your name: input("Enter your name again: ")
Enter your name again: dude

现在,当input("Enter your name: ")执行时,它等待用户输入,并且用户输入是有效的Python函数调用,因此也会被调用。这就是为什么我们Enter your name again:再次看到提示。

因此,您最好使用这样的raw_input功能

input_variable = raw_input("Enter your name: ")

如果需要将结果转换为其他类型,则可以使用适当的函数将所返回的字符串转换为raw_input。例如,要将输入读取为整数,请使用此答案中int所示的函数。

在python 3.x中,只有一个函数可以获取用户输入,该函数称为inputpython 2.7的raw_input

TL;DR

input function in Python 2.7, evaluates whatever your enter, as a Python expression. If you simply want to read strings, then use raw_input function in Python 2.7, which will not evaluate the read strings.

If you are using Python 3.x, raw_input has been renamed to input. Quoting the Python 3.0 release notes,

raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input())


In Python 2.7, there are two functions which can be used to accept user inputs. One is input and the other one is raw_input. You can think of the relation between them as follows

input = eval(raw_input)

Consider the following piece of code to understand this better

>>> dude = "thefourtheye"
>>> input_variable = input("Enter your name: ")
Enter your name: dude
>>> input_variable
'thefourtheye'

input accepts a string from the user and evaluates the string in the current Python context. When I type dude as input, it finds that dude is bound to the value thefourtheye and so the result of evaluation becomes thefourtheye and that gets assigned to input_variable.

If I enter something else which is not there in the current python context, it will fail will the NameError.

>>> input("Enter your name: ")
Enter your name: dummy
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'dummy' is not defined

Security considerations with Python 2.7’s input:

Since whatever user types is evaluated, it imposes security issues as well. For example, if you have already loaded os module in your program with import os, and then the user types in

os.remove("/etc/hosts")

this will be evaluated as a function call expression by python and it will be executed. If you are executing Python with elevated privileges, /etc/hosts file will be deleted. See, how dangerous it could be?

To demonstrate this, let’s try to execute input function again.

>>> dude = "thefourtheye"
>>> input("Enter your name: ")
Enter your name: input("Enter your name again: ")
Enter your name again: dude

Now, when input("Enter your name: ") is executed, it waits for the user input and the user input is a valid Python function invocation and so that is also invoked. That is why we are seeing Enter your name again: prompt again.

So, you are better off with raw_input function, like this

input_variable = raw_input("Enter your name: ")

If you need to convert the result to some other type, then you can use appropriate functions to convert the string returned by raw_input. For example, to read inputs as integers, use the int function, like shown in this answer.

In python 3.x, there is only one function to get user inputs and that is called input, which is equivalent to Python 2.7’s raw_input.


回答 1

您正在运行Python 2,而不是Python3。要在Python 2中运行,请使用raw_input

input_variable = raw_input ("Enter your name: ")
print ("your name is" + input_variable)

You are running Python 2, not Python 3. For this to work in Python 2, use raw_input.

input_variable = raw_input ("Enter your name: ")
print ("your name is" + input_variable)

回答 2

由于您是为Python 3.x编写的,因此您需要以以下内容开始脚本:

#!/usr/bin/env python3

如果您使用:

#!/usr/bin/env python

它将默认为Python2.x。如果没有以开头的内容,它们将放在脚本的第一行(又名shebang)。

如果您的脚本仅以以下内容开头:

#! python

然后,您可以将其更改为:

#! python3

尽管只有一些程序(例如启动器)可以识别这种较短的格式,但这并不是最佳选择。

前两个示例被广泛使用,将有助于确保您的代码在安装了Python的任何计算机上都能正常工作。

Since you are writing for Python 3.x, you’ll want to begin your script with:

#!/usr/bin/env python3

If you use:

#!/usr/bin/env python

It will default to Python 2.x. These go on the first line of your script, if there is nothing that starts with #! (aka the shebang).

If your scripts just start with:

#! python

Then you can change it to:

#! python3

Although this shorter formatting is only recognized by a few programs, such as the launcher, so it is not the best choice.

The first two examples are much more widely used and will help ensure your code will work on any machine that has Python installed.


回答 3

您应该使用,raw_input因为您使用的是python-2.7。在input()变量上使用时(例如s = input('Name: '):),它将在Python环境上执行命令,而不会保存您在变量(s)上写的内容,如果未定义写的内容,则会产生错误。

raw_input()会正确保存您在变量上写的内容(例如:)f = raw_input('Name : '),并且不会在Python环境中执行该操作而不会产生任何可能的错误:

input_variable = raw_input('Enter Your Name : ')
print("Your Name Is  : " + (input_variable))

You should use raw_input because you are using python-2.7. When you use input() on a variable (for example: s = input('Name: ')), it will execute the command ON the Python environment without saving what you wrote on the variable (s) and create an error if what you wrote is not defined.

raw_input() will save correctly what you wrote on the variable (for example: f = raw_input('Name : ')), and it will not execute it in the Python environment without creating any possible error:

input_variable = raw_input('Enter Your Name : ')
print("Your Name Is  : " + (input_variable))

回答 4

对于python 3及更高版本

s = raw_input()

如果您要在hackerrank网站上进行在线解决,它将解决pycharm IDE上的问题,然后使用:

s = input()

For python 3 and above

s = raw_input()

it will solve the problem on pycharm IDE if you are solving on online site exactly hackerrank then use:

s = input()

回答 5

您可以这样做:

x = raw_input("enter your name")
print "your name is %s " % x

要么:

x = str(input("enter your name"))
print "your name is %s" % x

You could either do:

x = raw_input("enter your name")
print "your name is %s " % x

or:

x = str(input("enter your name"))
print "your name is %s" % x

回答 6

input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

您必须输入单引号或双引号

Ex:'dude' -> correct

    dude -> not correct
input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

You have to enter input in either single or double quotes

Ex:'dude' -> correct

    dude -> not correct

回答 7

我还遇到了一个本应与python 2.7和3.7兼容的模块的问题

我发现解决问题的是导入:

from six.moves import input

这固定了两个口译员的可用性

你可以在这里阅读更多关于六个图书馆的信息

I also encountered this issue with a module that was supposed to be compatible for python 2.7 and 3.7

what i found to fix the issue was importing:

from six.moves import input

this fixed the usability for both interpreters

you can read more about the six library here


回答 8

我们正在使用以下适用于python 2和python 3的内容

#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Enter your name: "))

We are using the following that works both python 2 and python 3

#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Enter your name: "))

回答 9

对于可能会遇到此问题的其他任何人,事实证明,即使您#!/usr/bin/env python3在脚本的开头添加了该文件,如果该文件不可执行,也将忽略shebang。

要确定您的文件是否可执行:

  • 运行./filename.py在命令行
  • 如果得到-bash: ./filename.py: Permission denied,请运行chmod a+x filename.py
  • 再跑./filename.py一次

如果您已import sys; print(sys.version)按照Kevin的建议加入,现在您将看到python3正在解释该脚本

For anyone else that may run into this issue, turns out that even if you include #!/usr/bin/env python3 at the beginning of your script, the shebang is ignored if the file isn’t executable.

To determine whether or not your file is executable:

  • run ./filename.py from the command line
  • if you get -bash: ./filename.py: Permission denied, run chmod a+x filename.py
  • run ./filename.py again

If you’ve included import sys; print(sys.version) as Kevin suggested, you’ll now see that the script is being interpreted by python3


回答 10

以前的贡献不错。

import sys; print(sys.version)

def ingreso(nombre):
    print('Hi ', nombre, type(nombre))

def bienvenida(nombre):
    print("Hi "+nombre+", bye ")

nombre = raw_input("Enter your name: ")

ingreso(nombre)
bienvenida(nombre)

#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Your name: "))
输入您的姓名:Joe
(“嗨”,“乔”,<类型“ str”>)
嗨乔,再见 

您的名字:Joe
乔

谢谢!

Good contributions the previous ones.

import sys; print(sys.version)

def ingreso(nombre):
    print('Hi ', nombre, type(nombre))

def bienvenida(nombre):
    print("Hi "+nombre+", bye ")

nombre = raw_input("Enter your name: ")

ingreso(nombre)
bienvenida(nombre)

#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Your name: "))
Enter your name: Joe
('Hi ', 'Joe', &lttype 'str'&gt)
Hi Joe, bye 

Your name: Joe
Joe

Thanks!


回答 11

有两种方法可以解决这些问题,

  • 第一个很简单,无需更改代码,该代码
    由Python3运行,
    如果您仍然想在python2上运行,那么在运行python脚本后,输入输入时请记住

    1. 如果要输入,string则只需使用“ input go with double-quote”输入即可,它将在python2.7和
    2. 如果您想输入字符,请在输入中使用单引号,例如“您的输入在此处”
    3. 如果您想输入数字而不是问题,只需键入数字
  • 第二种方法是代码更改,
    使用以下导入并与任何版本的python一起运行

    1. from six.moves import input
    2. 在任何导入中使用raw_input()函数代替input()代码中的函数
    3. 使用str()like函数清理代码,str(input())然后将其分配给任何变量

错误提示
未定义名称“ dude”,即python的“ dude”在此处变为变量,并且没有分配任何python定义的类型的值,
因此只能像婴儿一样哭泣,因此如果我们定义“ dude”变量并分配任何值并传递给它,它将起作用,但这不是我们想要的,因为我们不知道用户将输入什么,此外,我们还希望捕获用户输入。

关于这些方法的事实:
input()函数:此函数按原样使用您输入的输入的值和类型,而无需修改其类型。
raw_input() 函数:此函数将您提供的输入明确转换为字符串类型,

注意:
input()方法的漏洞在于,任何人都可以使用变量或方法的名称来访问访问输入值的变量。

There are two ways to fix these issues,

  • 1st is simple without code change that is
    run your script by Python3,
    if you still want to run on python2 then after running your python script, when you are entering the input keep in mind

    1. if you want to enter string then just start typing down with “input goes with double-quote” and it will work in python2.7 and
    2. if you want to enter character then use the input with a single quote like ‘your input goes here’
    3. if you want to enter number not an issue you simply type the number
  • 2nd way is with code changes
    use the below import and run with any version of python

    1. from six.moves import input
    2. Use raw_input() function instead of input() function in your code with any import
    3. sanitise your code with str() function like str(input()) and then assign to any variable

As error implies:
name ‘dude’ is not defined i.e. for python ‘dude’ become variable here and it’s not having any value of python defined type assigned
so only its crying like baby so if we define a ‘dude’ variable and assign any value and pass to it, it will work but that’s not what we want as we don’t know what user will enter and moreover we want to capture the user input.

Fact about these method:
input() function: This function takes the value and type of the input you enter as it is without modifying it type.
raw_input() function: This function explicitly converts the input you give into type string,

Note:
The vulnerability in input() method lies in the fact that the variable accessing the value of input can be accessed by anyone just by using the name of variable or method.


回答 12

您可以更改在IDE中使用的python,如果您已经下载了python 3.x,则切换起来应该不太困难。但是您的脚本可以在python 3.x上正常工作,我只需要更改

print ("your name is" + input_variable)

print ("your name is", input_variable)

因为使用逗号,它会your name is在用户输入的内容与输入内容之间加上空格。AND:如果您使用的是2.7,请使用raw_input代替输入。

You can change which python you’re using with your IDE, if you’ve already downloaded python 3.x it shouldn’t be too hard to switch. But your script works fine on python 3.x, I would just change

print ("your name is" + input_variable)

to

print ("your name is", input_variable)

Because with the comma it prints with a whitespace in between your name is and whatever the user inputted. AND: if you’re using 2.7 just use raw_input instead of input.


如何在python中发送带有请求的“ multipart / form-data”?

问题:如何在python中发送带有请求的“ multipart / form-data”?

如何multipart/form-data在python中发送带有请求的请求?我了解如何发送文件,但是如何使用这种方法发送表单数据尚不清楚。

How to send a multipart/form-data with requests in python? How to send a file, I understand, but how to send the form data by this method can not understand.


回答 0

基本上,如果您指定files参数(字典),requests则将发送multipart/form-dataPOST而不是application/x-www-form-urlencodedPOST。您不限于在该词典中使用实际文件,但是:

>>> import requests
>>> response = requests.post('http://httpbin.org/post', files=dict(foo='bar'))
>>> response.status_code
200

httpbin.org可以让您知道您发布了哪些标题;在response.json()我们有:

>>> from pprint import pprint
>>> pprint(response.json()['headers'])
{'Accept': '*/*',
 'Accept-Encoding': 'gzip, deflate',
 'Connection': 'close',
 'Content-Length': '141',
 'Content-Type': 'multipart/form-data; '
                 'boundary=c7cbfdd911b4e720f1dd8f479c50bc7f',
 'Host': 'httpbin.org',
 'User-Agent': 'python-requests/2.21.0'}

更好的是,您可以通过使用元组而不是单个字符串或字节对象来进一步控制每个部分的文件名,内容类型和其他标题。元组应包含2到4个元素;文件名,内容,可选的内容类型以及其他标头的可选字典。

我将使用元组形式None作为文件名,以便filename="..."从那些部分的请求中删除参数:

>>> files = {'foo': 'bar'}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--bb3f05a247b43eede27a124ef8b968c5
Content-Disposition: form-data; name="foo"; filename="foo"

bar
--bb3f05a247b43eede27a124ef8b968c5--
>>> files = {'foo': (None, 'bar')}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--d5ca8c90a869c5ae31f70fa3ddb23c76
Content-Disposition: form-data; name="foo"

bar
--d5ca8c90a869c5ae31f70fa3ddb23c76--

files 如果需要排序和/或具有相同名称的多个字段,也可以是二值元组的列表:

requests.post(
    'http://requestb.in/xucj9exu',
    files=(
        ('foo', (None, 'bar')),
        ('foo', (None, 'baz')),
        ('spam', (None, 'eggs')),
    )
)

如果同时指定filesdata,然后它取决于价值data东西将被用于创建POST体。如果data是字符串,则仅使用它将;否则datafiles都使用和,data首先列出元素。

还有一个出色的requests-toolbelt项目,其中包括高级的Multipart支持。它采用与files参数格式相同的字段定义,但是与不同requests,它默认不设置文件名参数。另外,它可以从打开的文件对象流式传输请求,requests首先将在内存中构造请求主体:

from requests_toolbelt.multipart.encoder import MultipartEncoder

mp_encoder = MultipartEncoder(
    fields={
        'foo': 'bar',
        # plain file object, no filename or mime type produces a
        # Content-Disposition header with just the part name
        'spam': ('spam.txt', open('spam.txt', 'rb'), 'text/plain'),
    }
)
r = requests.post(
    'http://httpbin.org/post',
    data=mp_encoder,  # The MultipartEncoder is posted as data, don't use files=...!
    # The MultipartEncoder provides the content-type header with the boundary:
    headers={'Content-Type': mp_encoder.content_type}
)

字段遵循相同的约定;使用包含2到4个元素的元组来添加文件名,部分mime类型或额外的标头。不像files参数,没有试图找到一个默认filename值,如果你不使用的元组。

Basically, if you specify a files parameter (a dictionary), then requests will send a multipart/form-data POST instead of a application/x-www-form-urlencoded POST. You are not limited to using actual files in that dictionary, however:

>>> import requests
>>> response = requests.post('http://httpbin.org/post', files=dict(foo='bar'))
>>> response.status_code
200

and httpbin.org lets you know what headers you posted with; in response.json() we have:

>>> from pprint import pprint
>>> pprint(response.json()['headers'])
{'Accept': '*/*',
 'Accept-Encoding': 'gzip, deflate',
 'Connection': 'close',
 'Content-Length': '141',
 'Content-Type': 'multipart/form-data; '
                 'boundary=c7cbfdd911b4e720f1dd8f479c50bc7f',
 'Host': 'httpbin.org',
 'User-Agent': 'python-requests/2.21.0'}

Better still, you can further control the filename, content type and additional headers for each part by using a tuple instead of a single string or bytes object. The tuple is expected to contain between 2 and 4 elements; the filename, the content, optionally a content type, and an optional dictionary of further headers.

I’d use the tuple form with None as the filename, so that the filename="..." parameter is dropped from the request for those parts:

>>> files = {'foo': 'bar'}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--bb3f05a247b43eede27a124ef8b968c5
Content-Disposition: form-data; name="foo"; filename="foo"

bar
--bb3f05a247b43eede27a124ef8b968c5--
>>> files = {'foo': (None, 'bar')}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--d5ca8c90a869c5ae31f70fa3ddb23c76
Content-Disposition: form-data; name="foo"

bar
--d5ca8c90a869c5ae31f70fa3ddb23c76--

files can also be a list of two-value tuples, if you need ordering and/or multiple fields with the same name:

requests.post(
    'http://requestb.in/xucj9exu',
    files=(
        ('foo', (None, 'bar')),
        ('foo', (None, 'baz')),
        ('spam', (None, 'eggs')),
    )
)

If you specify both files and data, then it depends on the value of data what will be used to create the POST body. If data is a string, only it willl be used; otherwise both data and files are used, with the elements in data listed first.

There is also the excellent requests-toolbelt project, which includes advanced Multipart support. It takes field definitions in the same format as the files parameter, but unlike requests, it defaults to not setting a filename parameter. In addition, it can stream the request from open file objects, where requests will first construct the request body in memory:

from requests_toolbelt.multipart.encoder import MultipartEncoder

mp_encoder = MultipartEncoder(
    fields={
        'foo': 'bar',
        # plain file object, no filename or mime type produces a
        # Content-Disposition header with just the part name
        'spam': ('spam.txt', open('spam.txt', 'rb'), 'text/plain'),
    }
)
r = requests.post(
    'http://httpbin.org/post',
    data=mp_encoder,  # The MultipartEncoder is posted as data, don't use files=...!
    # The MultipartEncoder provides the content-type header with the boundary:
    headers={'Content-Type': mp_encoder.content_type}
)

Fields follow the same conventions; use a tuple with between 2 and 4 elements to add a filename, part mime-type or extra headers. Unlike the files parameter, no attempt is made to find a default filename value if you don’t use a tuple.


回答 1

自从编写了先前的答案以来,请求已更改。请查看Github上的bug线程以获取更多详细信息,并以示例的形式发表评论

简而言之,files参数使用a dict作为参数,键是表单字段的名称,值是字符串或2、3或4个长度的元组,如在请求中发布多部分编码文件一节中所述快速开始:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

在上面,元组的组成如下:

(filename, data, content_type, headers)

如果该值只是一个字符串,则文件名将与键相同,如下所示:

>>> files = {'obvius_session_id': '72c2b6f406cdabd578c5fd7598557c52'}

Content-Disposition: form-data; name="obvius_session_id"; filename="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52

如果值是一个元组,并且第一个条目是Nonefilename属性,将不包括在内:

>>> files = {'obvius_session_id': (None, '72c2b6f406cdabd578c5fd7598557c52')}

Content-Disposition: form-data; name="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52

Since the previous answers were written, requests have changed. Have a look at the bug thread at Github for more detail and this comment for an example.

In short, the files parameter takes a dict with the key being the name of the form field and the value being either a string or a 2, 3 or 4-length tuple, as described in the section POST a Multipart-Encoded File in the requests quickstart:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

In the above, the tuple is composed as follows:

(filename, data, content_type, headers)

If the value is just a string, the filename will be the same as the key, as in the following:

>>> files = {'obvius_session_id': '72c2b6f406cdabd578c5fd7598557c52'}

Content-Disposition: form-data; name="obvius_session_id"; filename="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52

If the value is a tuple and the first entry is None the filename property will not be included:

>>> files = {'obvius_session_id': (None, '72c2b6f406cdabd578c5fd7598557c52')}

Content-Disposition: form-data; name="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52

回答 2

即使不需要上传任何文件,也需要使用该files参数发送多部分表单POST请求。

从原始请求来源:

def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.

    ...
    :param files: (optional) Dictionary of ``'name': file-like-objects``
        (or ``{'name': file-tuple}``) for multipart encoding upload.
        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``,
        3-tuple ``('filename', fileobj, 'content_type')``
        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``,
        where ``'content-type'`` is a string
        defining the content type of the given file
        and ``custom_headers`` a dict-like object 
        containing additional headers to add for the file.

相关部分是: file-tuple can be a2-tuple,。3-tupleor a4-tuple

基于上述内容,最简单的多部分表单请求包括要上传的文件和表单字段,如下所示:

multipart_form_data = {
    'file2': ('custom_file_name.zip', open('myfile.zip', 'rb')),
    'action': (None, 'store'),
    'path': (None, '/path1')
}

response = requests.post('https://httpbin.org/post', files=multipart_form_data)

print(response.content)

请注意,None作为纯文本字段的元组中的第一个参数-这是文件名字段的占位符,仅用于文件上传,但对于文本字段,传递None第一个参数是必需的,以便提交数据。

具有相同名称的多个字段

如果您需要发布多个具有相同名称的字段,那么您可以将有效负载定义为元组列表(或元组),而不是字典:

multipart_form_data = (
    ('file2', ('custom_file_name.zip', open('myfile.zip', 'rb'))),
    ('action', (None, 'store')),
    ('path', (None, '/path1')),
    ('path', (None, '/path2')),
    ('path', (None, '/path3')),
)

流请求API

如果上述API对您来说还不够Python,那么请考虑使用request工具带pip install requests_toolbelt),它是核心请求模块的扩展,该模块提供对文件上传流的支持以及MultipartEncoder(可以代替来使用)files,并且还可以您可以将有效负载定义为字典,元组或列表。

MultipartEncoder可以用于有或没有实际上传字段的多部分请求。必须将其分配给data参数。

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

multipart_data = MultipartEncoder(
    fields={
            # a file upload field
            'file': ('file.zip', open('file.zip', 'rb'), 'text/plain')
            # plain text fields
            'field0': 'value0', 
            'field1': 'value1',
           }
    )

response = requests.post('http://httpbin.org/post', data=multipart_data,
                  headers={'Content-Type': multipart_data.content_type})

如果您需要发送多个具有相同名称的字段,或者表单字段的顺序很重要,则可以使用元组或列表代替字典:

multipart_data = MultipartEncoder(
    fields=(
            ('action', 'ingest'), 
            ('item', 'spam'),
            ('item', 'sausage'),
            ('item', 'eggs'),
           )
    )

You need to use the files parameter to send a multipart form POST request even when you do not need to upload any files.

From the original requests source:

def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.

    ...
    :param files: (optional) Dictionary of ``'name': file-like-objects``
        (or ``{'name': file-tuple}``) for multipart encoding upload.
        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``,
        3-tuple ``('filename', fileobj, 'content_type')``
        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``,
        where ``'content-type'`` is a string
        defining the content type of the given file
        and ``custom_headers`` a dict-like object 
        containing additional headers to add for the file.

The relevant part is: file-tuple can be a2-tuple, 3-tupleor a4-tuple.

Based on the above, the simplest multipart form request that includes both files to upload and form fields will look like this:

multipart_form_data = {
    'file2': ('custom_file_name.zip', open('myfile.zip', 'rb')),
    'action': (None, 'store'),
    'path': (None, '/path1')
}

response = requests.post('https://httpbin.org/post', files=multipart_form_data)

print(response.content)

Note the None as the first argument in the tuple for plain text fields — this is a placeholder for the filename field which is only used for file uploads, but for text fields passing None as the first parameter is required in order for the data to be submitted.

Multiple fields with the same name

If you need to post multiple fields with the same name then instead of a dictionary you can define your payload as a list (or a tuple) of tuples:

multipart_form_data = (
    ('file2', ('custom_file_name.zip', open('myfile.zip', 'rb'))),
    ('action', (None, 'store')),
    ('path', (None, '/path1')),
    ('path', (None, '/path2')),
    ('path', (None, '/path3')),
)

Streaming requests API

If the above API is not pythonic enough for you, then consider using requests toolbelt (pip install requests_toolbelt) which is an extension of the core requests module that provides support for file upload streaming as well as the MultipartEncoder which can be used instead of files, and which also lets you define the payload as a dictionary, tuple or list.

MultipartEncoder can be used both for multipart requests with or without actual upload fields. It must be assigned to the data parameter.

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

multipart_data = MultipartEncoder(
    fields={
            # a file upload field
            'file': ('file.zip', open('file.zip', 'rb'), 'text/plain')
            # plain text fields
            'field0': 'value0', 
            'field1': 'value1',
           }
    )

response = requests.post('http://httpbin.org/post', data=multipart_data,
                  headers={'Content-Type': multipart_data.content_type})

If you need to send multiple fields with the same name, or if the order of form fields is important, then a tuple or a list can be used instead of a dictionary:

multipart_data = MultipartEncoder(
    fields=(
            ('action', 'ingest'), 
            ('item', 'spam'),
            ('item', 'sausage'),
            ('item', 'eggs'),
           )
    )

回答 3

以下是使用请求上传带有其他参数的单个文件的简单代码段:

url = 'https://<file_upload_url>'
fp = '/Users/jainik/Desktop/data.csv'

files = {'file': open(fp, 'rb')}
payload = {'file_id': '1234'}

response = requests.put(url, files=files, data=payload, verify=False)

请注意,您不需要显式指定任何内容类型。

注意:想对以上答案之一发表评论,但由于声誉不佳而无法发表评论,因此在此处起草了一个新的答复。

Here is the simple code snippet to upload a single file with additional parameters using requests:

url = 'https://<file_upload_url>'
fp = '/Users/jainik/Desktop/data.csv'

files = {'file': open(fp, 'rb')}
payload = {'file_id': '1234'}

response = requests.put(url, files=files, data=payload, verify=False)

Please note that you don’t need to explicitly specify any content type.

NOTE: Wanted to comment on one of the above answers but could not because of low reputation so drafted a new response here.


回答 4

您需要使用name网站HTML中的上传文件的属性。例:

autocomplete="off" name="image">

看到了 name="image">吗 您可以在用于上传文件的网站的HTML中找到它。您需要使用它来上传文件Multipart/form-data

脚本:

import requests

site = 'https://prnt.sc/upload.php' # the site where you upload the file
filename = 'image.jpg'  # name example

在这里,在图片的位置,以HTML添加上传文件的名称

up = {'image':(filename, open(filename, 'rb'), "multipart/form-data")}

如果上传需要单击上传按钮,则可以这样使用:

data = {
     "Button" : "Submit",
}

然后开始请求

request = requests.post(site, files=up, data=data)

完成,文件成功上传

You need to use the name attribute of the upload file that is in the HTML of the site. Example:

autocomplete="off" name="image">

You see name="image">? You can find it in the HTML of a site for uploading the file. You need to use it to upload the file with Multipart/form-data

script:

import requests

site = 'https://prnt.sc/upload.php' # the site where you upload the file
filename = 'image.jpg'  # name example

Here, in the place of image, add the name of the upload file in HTML

up = {'image':(filename, open(filename, 'rb'), "multipart/form-data")}

If the upload requires to click the button for upload, you can use like that:

data = {
     "Button" : "Submit",
}

Then start the request

request = requests.post(site, files=up, data=data)

And done, file uploaded succesfully


回答 5

发送多部分/表单数据键和值

curl命令:

curl -X PUT http://127.0.0.1:8080/api/xxx ...
-H 'content-type: multipart/form-data; boundary=----xxx' \
-F taskStatus=1

python 请求-更复杂的POST请求

    updateTaskUrl = "http://127.0.0.1:8080/api/xxx"
    updateInfoDict = {
        "taskStatus": 1,
    }
    resp = requests.put(updateTaskUrl, data=updateInfoDict)

发送多部分/表单数据文件

curl命令:

curl -X POST http://127.0.0.1:8080/api/xxx ...
-H 'content-type: multipart/form-data; boundary=----xxx' \
-F file=@/Users/xxx.txt

python 请求-发布多部分编码的文件

    filePath = "/Users/xxx.txt"
    fileFp = open(filePath, 'rb')
    fileInfoDict = {
        "file": fileFp,
    }
    resp = requests.post(uploadResultUrl, files=fileInfoDict)

就这样。

Send multipart/form-data key and value

curl command:

curl -X PUT http://127.0.0.1:8080/api/xxx ...
-H 'content-type: multipart/form-data; boundary=----xxx' \
-F taskStatus=1

python requests – More complicated POST requests:

    updateTaskUrl = "http://127.0.0.1:8080/api/xxx"
    updateInfoDict = {
        "taskStatus": 1,
    }
    resp = requests.put(updateTaskUrl, data=updateInfoDict)

Send multipart/form-data file

curl command:

curl -X POST http://127.0.0.1:8080/api/xxx ...
-H 'content-type: multipart/form-data; boundary=----xxx' \
-F file=@/Users/xxx.txt

python requests – POST a Multipart-Encoded File:

    filePath = "/Users/xxx.txt"
    fileFp = open(filePath, 'rb')
    fileInfoDict = {
        "file": fileFp,
    }
    resp = requests.post(uploadResultUrl, files=fileInfoDict)

that’s all.


回答 6

这是您需要将一个大的单个文件作为多部分表单数据上传的python代码段。使用NodeJs Multer中间件在服务器端运行。

import requests
latest_file = 'path/to/file'
url = "http://httpbin.org/apiToUpload"
files = {'fieldName': open(latest_file, 'rb')}
r = requests.put(url, files=files)

对于服务器端,请在以下位置查看multer文档:https : //github.com/expressjs/multer, 此处字段single(’fieldName’)用于接受一个文件,如下所示:

var upload = multer().single('fieldName');

Here is the python snippet you need to upload one large single file as multipart formdata. With NodeJs Multer middleware running on the server side.

import requests
latest_file = 'path/to/file'
url = "http://httpbin.org/apiToUpload"
files = {'fieldName': open(latest_file, 'rb')}
r = requests.put(url, files=files)

For the server side please check the multer documentation at: https://github.com/expressjs/multer here the field single(‘fieldName’) is used to accept one single file, as in:

var upload = multer().single('fieldName');

使用Python编辑CSV文件时跳过标题

问题:使用Python编辑CSV文件时跳过标题

我正在使用以下引用的代码使用Python编辑CSV。代码中调用的函数构成了代码的上部。

问题:我希望下面引用的代码从第二行开始编辑csv,我希望它排除包含标题的第一行。现在,它仅在第一行上应用函数,并且我的标题行正在更改。

in_file = open("tmob_notcleaned.csv", "rb")
reader = csv.reader(in_file)
out_file = open("tmob_cleaned.csv", "wb")
writer = csv.writer(out_file)
row = 1
for row in reader:
    row[13] = handle_color(row[10])[1].replace(" - ","").strip()
    row[10] = handle_color(row[10])[0].replace("-","").replace("(","").replace(")","").strip()
    row[14] = handle_gb(row[10])[1].replace("-","").replace(" ","").replace("GB","").strip()
    row[10] = handle_gb(row[10])[0].strip()
    row[9] = handle_oem(row[10])[1].replace("Blackberry","RIM").replace("TMobile","T-Mobile").strip()
    row[15] = handle_addon(row[10])[1].strip()
    row[10] = handle_addon(row[10])[0].replace(" by","").replace("FREE","").strip()
    writer.writerow(row)
in_file.close()    
out_file.close()

我试图通过将row变量初始化为来解决此问题,1但没有成功。

请帮助我解决这个问题。

I am using below referred code to edit a csv using Python. Functions called in the code form upper part of the code.

Problem: I want the below referred code to start editing the csv from 2nd row, I want it to exclude 1st row which contains headers. Right now it is applying the functions on 1st row only and my header row is getting changed.

in_file = open("tmob_notcleaned.csv", "rb")
reader = csv.reader(in_file)
out_file = open("tmob_cleaned.csv", "wb")
writer = csv.writer(out_file)
row = 1
for row in reader:
    row[13] = handle_color(row[10])[1].replace(" - ","").strip()
    row[10] = handle_color(row[10])[0].replace("-","").replace("(","").replace(")","").strip()
    row[14] = handle_gb(row[10])[1].replace("-","").replace(" ","").replace("GB","").strip()
    row[10] = handle_gb(row[10])[0].strip()
    row[9] = handle_oem(row[10])[1].replace("Blackberry","RIM").replace("TMobile","T-Mobile").strip()
    row[15] = handle_addon(row[10])[1].strip()
    row[10] = handle_addon(row[10])[0].replace(" by","").replace("FREE","").strip()
    writer.writerow(row)
in_file.close()    
out_file.close()

I tried to solve this problem by initializing row variable to 1 but it didn’t work.

Please help me in solving this issue.


回答 0

您的reader变量是可迭代的,通过循环它可以检索行。

要使其在循环前跳过一项,只需调用next(reader, None)并忽略返回值即可。

您还可以稍微简化代码;使用打开的文件作为上下文管理器可以自动关闭它们:

with open("tmob_notcleaned.csv", "rb") as infile, open("tmob_cleaned.csv", "wb") as outfile:
   reader = csv.reader(infile)
   next(reader, None)  # skip the headers
   writer = csv.writer(outfile)
   for row in reader:
       # process each row
       writer.writerow(row)

# no need to close, the files are closed automatically when you get to this point.

如果您想将标头写入未经处理的输出文件,也很容易,请将输出传递next()writer.writerow()

headers = next(reader, None)  # returns the headers or `None` if the input is empty
if headers:
    writer.writerow(headers)

Your reader variable is an iterable, by looping over it you retrieve the rows.

To make it skip one item before your loop, simply call next(reader, None) and ignore the return value.

You can also simplify your code a little; use the opened files as context managers to have them closed automatically:

with open("tmob_notcleaned.csv", "rb") as infile, open("tmob_cleaned.csv", "wb") as outfile:
   reader = csv.reader(infile)
   next(reader, None)  # skip the headers
   writer = csv.writer(outfile)
   for row in reader:
       # process each row
       writer.writerow(row)

# no need to close, the files are closed automatically when you get to this point.

If you wanted to write the header to the output file unprocessed, that’s easy too, pass the output of next() to writer.writerow():

headers = next(reader, None)  # returns the headers or `None` if the input is empty
if headers:
    writer.writerow(headers)

回答 1

解决此问题的另一种方法是使用DictReader类,该类“跳过”标题行并将其用于允许命名索引。

给定“ foo.csv”,如下所示:

FirstColumn,SecondColumn
asdf,1234
qwer,5678

像这样使用DictReader:

import csv
with open('foo.csv') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        print(row['FirstColumn'])  # Access by column header instead of column number
        print(row['SecondColumn'])

Another way of solving this is to use the DictReader class, which “skips” the header row and uses it to allowed named indexing.

Given “foo.csv” as follows:

FirstColumn,SecondColumn
asdf,1234
qwer,5678

Use DictReader like this:

import csv
with open('foo.csv') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        print(row['FirstColumn'])  # Access by column header instead of column number
        print(row['SecondColumn'])

回答 2

在做 row=1不会改变任何东西,因为您只会用循环的结果覆盖它。

您要next(reader)跳过一行。

Doing row=1 won’t change anything, because you’ll just overwrite that with the results of the loop.

You want to do next(reader) to skip one row.


Python请求-无连接适配器

问题:Python请求-无连接适配器

我正在使用Requests:HTTP for Humans库,但出现了这个奇怪的错误,我不知道这是什么意思。

No connection adapters were found for '192.168.1.61:8080/api/call'

有人有主意吗?

I’m using the Requests: HTTP for Humans library and I got this weird error and I don’t know what is mean.

No connection adapters were found for '192.168.1.61:8080/api/call'

Anybody has an idea?


回答 0

您需要包括协议方案:

'http://192.168.1.61:8080/api/call'

没有这个http://部分,requests就不知道如何连接到远程服务器。

请注意,协议方案必须全部为小写。如果您的URL以HTTP://例如开头,则也找不到http://连接适配器。

You need to include the protocol scheme:

'http://192.168.1.61:8080/api/call'

Without the http:// part, requests has no idea how to connect to the remote server.

Note that the protocol scheme must be all lowercase; if your URL starts with HTTP:// for example, it won’t find the http:// connection adapter either.


回答 1

另一个原因,也许您的URL包含一些隐藏的字符,例如’\ n’。

如果您按如下方式定义网址,则会引发此异常:

url = '''
http://google.com
'''

因为字符串中有’\ n’隐藏。该网址实际上变为:

\nhttp://google.com\n

One more reason, maybe your url include some hiden characters, such as ‘\n’.

If you define your url like below, this exception will raise:

url = '''
http://google.com
'''

because there are ‘\n’ hide in the string. The url in fact become:

\nhttp://google.com\n

在OSX 10.11(El Capitan)中安装Scrapy(系统完整性保护)时,出现“ OSError:[Errno 1] Operation not allow”

问题:在OSX 10.11(El Capitan)中安装Scrapy(系统完整性保护)时,出现“ OSError:[Errno 1] Operation not allow”

我正在尝试通过pip在OSX 10.11(El Capitan)中安装Scrapy Python框架。安装脚本下载所需的模块,并在某些时候返回以下错误:

OSError: [Errno 1] Operation not permitted: '/tmp/pip-nIfswi-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

我尝试使用以下命令停用OSX 10.11中的无根功能:

sudo nvram boot-args="rootless=0";sudo reboot

但是当机器重启时,我仍然收到相同的错误。

我的StackExchangers同事有什么线索或想法吗?

如果有帮助,则完整的脚本输出如下:

sudo -s pip install scrapy
Collecting scrapy
  Downloading Scrapy-1.0.2-py2-none-any.whl (290kB)
    100% |████████████████████████████████| 290kB 345kB/s 
Requirement already satisfied (use --upgrade to upgrade): cssselect>=0.9 in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): queuelib in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): pyOpenSSL in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from scrapy)
Collecting w3lib>=1.8.0 (from scrapy)
  Downloading w3lib-1.12.0-py2.py3-none-any.whl
Collecting lxml (from scrapy)
  Downloading lxml-3.4.4.tar.gz (3.5MB)
    100% |████████████████████████████████| 3.5MB 112kB/s 
Collecting Twisted>=10.0.0 (from scrapy)
  Downloading Twisted-15.3.0.tar.bz2 (4.4MB)
    100% |████████████████████████████████| 4.4MB 94kB/s 
Collecting six>=1.5.2 (from scrapy)
  Downloading six-1.9.0-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from Twisted>=10.0.0->scrapy)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from zope.interface>=3.6.0->Twisted>=10.0.0->scrapy)
Installing collected packages: six, w3lib, lxml, Twisted, scrapy
  Found existing installation: six 1.4.1
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.4.1:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/basecommand.py", line 223, in main
status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/commands/install.py", line 299, in run
root=options.root_path,
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_set.py", line 640, in install
requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_install.py", line 726, in uninstall
paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_uninstall.py", line 125, in remove
renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/utils/__init__.py", line 314, in renames
shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-nIfswi-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

I’m trying to install Scrapy Python framework in OSX 10.11 (El Capitan) via pip. The installation script downloads the required modules and at some point returns the following error:

OSError: [Errno 1] Operation not permitted: '/tmp/pip-nIfswi-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

I’ve tried to deactivate the rootless feature in OSX 10.11 with the command:

sudo nvram boot-args="rootless=0";sudo reboot

but I still get the same error when the machine reboots.

Any clue or idea from my fellow StackExchangers?

If it helps, the full script output is the following:

sudo -s pip install scrapy
Collecting scrapy
  Downloading Scrapy-1.0.2-py2-none-any.whl (290kB)
    100% |████████████████████████████████| 290kB 345kB/s 
Requirement already satisfied (use --upgrade to upgrade): cssselect>=0.9 in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): queuelib in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): pyOpenSSL in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from scrapy)
Collecting w3lib>=1.8.0 (from scrapy)
  Downloading w3lib-1.12.0-py2.py3-none-any.whl
Collecting lxml (from scrapy)
  Downloading lxml-3.4.4.tar.gz (3.5MB)
    100% |████████████████████████████████| 3.5MB 112kB/s 
Collecting Twisted>=10.0.0 (from scrapy)
  Downloading Twisted-15.3.0.tar.bz2 (4.4MB)
    100% |████████████████████████████████| 4.4MB 94kB/s 
Collecting six>=1.5.2 (from scrapy)
  Downloading six-1.9.0-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from Twisted>=10.0.0->scrapy)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from zope.interface>=3.6.0->Twisted>=10.0.0->scrapy)
Installing collected packages: six, w3lib, lxml, Twisted, scrapy
  Found existing installation: six 1.4.1
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.4.1:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/basecommand.py", line 223, in main
status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/commands/install.py", line 299, in run
root=options.root_path,
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_set.py", line 640, in install
requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_install.py", line 726, in uninstall
paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_uninstall.py", line 125, in remove
renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg/pip/utils/__init__.py", line 314, in renames
shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-nIfswi-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

回答 0

我也认为完全没有必要开始入侵OSX。

我能够解决它

brew install python

看来,使用新版El Capitan随附的python / pip会有一些问题。

I also think it’s absolutely not necessary to start hacking OS X.

I was able to solve it doing a

brew install python

It seems that using the python / pip that comes with new El Capitan has some issues.


回答 1

pip install --ignore-installed six

会做到的。

资料来源:github.com/pypa/pip/issues/3165

pip install --ignore-installed six

Would do the trick.

Source: github.com/pypa/pip/issues/3165


回答 2

正如其他答案所说,这是由于新的系统完整性保护,但我认为其他答案过于复杂。

如果您只想在当前用户中使用该软件包,则可以使用该--user标志,而无需禁用SIP,就可以很好地安装它。像这样:

sudo pip install --user packagename

As the other answers said, it’s because of the new System Integrity Protection, but I believe the other answers are overcomplicated.

If you’re only gonna use that package in the current user, you should be able to install it just fine, without the need to disable the SIP, by using the --user flag. Like this:

sudo pip install --user packagename

回答 3

高投票答案对我不起作用,似乎对El Capitan用户有效。但是对于MacOS Sierra用户,请尝试以下步骤

  1. brew install python
  2. sudo pip install --user <package name>

The high voted answers didn’t work for me, it seems to work for El Capitan users. But for MacOS Sierra users try the following steps

  1. brew install python
  2. sudo pip install --user <package name>

回答 4

警告事项

强烈建议不要在Mac上修改系统Python;可能会发生许多问题。

您的特定错误表明安装程序存在解决Scrapy依赖关系的问题,而不影响当前的Python安装。该系统使用Python执行许多基本任务,因此保持系统安装的稳定性和Apple最初安装的位置非常重要

在绕过内置安全性之前,我还将穷尽所有其他可能性

包管理器解决方案:

请先研究Python虚拟化工具,例如virtualenv;这样可以安全地进行实验。

在不与Mac OS冲突的情况下使用语言和软件的另一个有用工具是Homebrew。与MacPortsFink一样Homebrew是Mac的软件包管理器。,对于安全尝试许多其他语言和工具很有用。

“自行运行”软件安装:

如果您不喜欢包管理器方法,则可以使用该/usr/local路径或创建/opt/local用于安装备用Python安装的目录,然后在中修复路径.bashrc。请注意,您必须为这些解决方案启用root。

无论如何如何:

如果您绝对必须禁用安全检查(并且我衷心希望它用于解决系统语言和资源之外的其他问题),则可以暂时禁用它,然后使用本文中有关如何禁用系统的一些技术来重新启用它完整性保护

Warnings

I would suggest very strongly against modifying the system Python on Mac; there are numerous issues that can occur.

Your particular error shows that the installer has issues resolving the dependencies for Scrapy without impacting the current Python installation. The system uses Python for a number of essential tasks, so it’s important to keep the system installation stable and as originally installed by Apple.

I would also exhaust all other possibilities before bypassing built in security.

Package Manager Solutions:

Please look into a Python virtualization tool such as virtualenv first; this will allow you to experiment safely.

Another useful tool to use languages and software without conflicting with your Mac OS is Homebrew. Like MacPorts or Fink, Homebrew is a package manager for Mac, and is useful for safely trying lots of other languages and tools.

“Roll your own” Software Installs:

If you don’t like the package manager approach, you could use the /usr/local path or create an /opt/local directory for installing an alternate Python installation and fix up your paths in your .bashrc. Note that you’ll have to enable root for these solutions.

How to do it anyway:

If you absolutely must disable the security check (and I sincerely hope it’s for something other than messing with the system languages and resources), you can disable it temporarily and re-enable it using some of the techniques in this post on how to Disable System Integrity-Protection.


回答 5

这对我有用:

   sudo pip install scrapy --ignore-installed six

This did the trick for me:

   sudo pip install scrapy --ignore-installed six

回答 6

您应禁用“系统完整性保护”,这是El Capitan的一项新功能。

首先,您应该在终端上运行无根配置命令

# nvram boot-args="rootless=0"
# reboot

然后,您应该在恢复分区的终端(恢复操作系统)上运行以下命令

# csrutil disable
# reboot

我刚刚解决了我的问题。我不确定第一部分是否必要。随便尝试。

– 警告

一切正常后,您应该再次启用SIP。

只需再次重新启动进入恢复模式并在终端中运行

# csrutil enable

csrutil:配置系统完整性保护

You should disable “System Integrity Protection” which is a new feature in El Capitan.

First, you should run the command for rootless config on your terminal

# nvram boot-args="rootless=0"
# reboot

Then, you should run the command below on recovery partition’s terminal (Recovery OS)

# csrutil disable
# reboot

I’ve just solved my problem like that. I’m not sure that the first part is necessary. Try as you like.

–WARNING

You should enable SIP again after everything works;

Simply reboot again into Recovery Mode and run in terminal

# csrutil enable

csrutil: Configuring System Integrity Protection


回答 7

我试图通过El Capitan中的pip安装AWS,但出现此错误

OSError:[Errno 1]不允许操作:’/var/folders/wm/jhnj0g_s16gb36y8kwvrgm7h0000gp/T/pip-wTnb_D-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six- 1.4.1-py2.7.egg-info’

我在这里找到答案

sudo -H pip install awscli --upgrade --ignore-installed six

这个对我有用 :)

I tried to install AWS via pip in El Capitan but this error appear

OSError: [Errno 1] Operation not permitted: ‘/var/folders/wm/jhnj0g_s16gb36y8kwvrgm7h0000gp/T/pip-wTnb_D-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info’

I found the answer here

sudo -H pip install awscli --upgrade --ignore-installed six

It works for me :)


回答 8

我在MacOS Sierra上遇到了相同的错误。我按照以下步骤操作,成功安装了可伸缩的软件包。

1. sudo pip install --ignore-installed six
2. sudo pip install --ignore-installed scrapy

MacBook-Air:~ shree$ scrapy version
Scrapy 1.4.0

I was getting the same error on on my MacOS Sierra. I followed these steps and successfully able to install scarpy package.

1. sudo pip install --ignore-installed six
2. sudo pip install --ignore-installed scrapy

MacBook-Air:~ shree$ scrapy version
Scrapy 1.4.0

回答 9

这对我有用。

sudo pip install –ignore-installed scrapy

This did the trick for me.

sudo pip install –ignore-installed scrapy


回答 10

尝试了一些答案的组合,最终成功了:

sudo -H pip install --upgrade --ignore-installed awsebcli

干杯

Tried a combination of some answers and this eventually worked:

sudo -H pip install --upgrade --ignore-installed awsebcli

Cheers


回答 11

再次安装python:

brew安装python

再试一次:

须藤点安装刮

为我工作,希望它可以帮助

install python again:

brew install python

try it again:

sudo pip install scrapy

works for me, hope it can help


回答 12

重新启动Mac->在启动提示音后按住“ Command + R”->打开OS X实用程序->打开终端并键入“ csrutil disable”->重新启动OS X->打开终端并检查“ csrutil status”

Restart Mac -> hold down “Command + R” after the startup chime -> Opens OS X Utilities -> Open Terminal and type “csrutil disable” -> Reboot OS X -> Open Terminal and check “csrutil status”


回答 13

这个命令可以很好地工作:D

sudo -H pip install –upgrade package_name –ignore-installed六

This command would work perfectly fine :D

sudo -H pip install –upgrade package_name –ignore-installed six


回答 14

如果您尝试使用pip而不是pip3在python2文件夹中安装python3 lib,有时可能会实现这种行为。

Sometimes such behavior may be achieved if you try to install python3 lib in python2 folder using pip instead of pip3.


回答 15

  1. -关闭SIP(系统完整性保护)-然后重新启动,使用命令+ R进入调试模式,然后选择终端:csrutil disable reboot

2。

sudo C_INCLUDE_PATH = /应用程序/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX .platform / Developer / SDKs / MacOSX10.11.sdk / usr / include / libxml2 / libxml:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/包括pip install scrapy –ignore-installed 6

3.-然后删除旧的六个,重新安装sudo rm -rf /Library/Python/2.7/site-packages/six* sudo rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/ lib / python / six * sudo pip安装六

4.-然后将其设置回csrutil启用重新启动

-cr脚的作品现在

  1. — close SIP(system Integrity Protection) — then reboot, use command +R to enter debug mode, then select terminal: csrutil disable reboot

2.

sudo C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2 :/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/libxml :/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include pip install scrapy –ignore-installed six

3. — then remove old six, install it again sudo rm -rf /Library/Python/2.7/site-packages/six* sudo rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six* sudo pip install six

4. — then set it back csrutil enable reboot

— crappy works now


回答 16

它对我有用:

pip install scrapy --user -U

it work for me:

pip install scrapy --user -U

回答 17

我在其他地方缺少依赖项,因此我为项目安装了其他要求,如下所示:

pip install --user -r requirements.txt

I was missing a dependency somewhere else along the line, so I installed the other requirements for the project like this:

pip install --user -r requirements.txt

如何在Python 3.x和Python 2.x上使用pip

问题:如何在Python 3.x和Python 2.x上使用pip

我安装了Python 3.x(在Ubuntu上除了Python 2.x之外),然后慢慢开始配对在Python 2.x中使用的模块。

所以我想知道,应该为Python 2.x和Python 3.x使用pip来简化生活吗?

I installed Python 3.x (besides Python 2.x on Ubuntu) and slowly started to pair modules I use in Python 2.x.

So I wonder, what approach should I take to make my life easy by using pip for both Python 2.x and Python 3.x?


回答 0

您应该采取的方法是安装 pip为Python 3.2。

您可以通过以下方式执行此操作:

$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python3.2 get-pip.py

然后,您可以使用来安装适用于Python 3.2的内容pip-3.2,并使用来安装适用于Python 2-7的内容pip-2.7。该pip命令最终将指向其中之一,但是我不确定是哪一个,因此您必须进行检查。

The approach you should take is to install pip for Python 3.2.

You do this in the following way:

$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python3.2 get-pip.py

Then, you can install things for Python 3.2 with pip-3.2, and install things for Python 2-7 with pip-2.7. The pip command will end up pointing to one of these, but I’m not sure which, so you will have to check.


回答 1

您还可以做的是使用apt-get:

apt-get install python3-pip

以我的经验,这也很流利,而且您会从apt-get中获得所有好处。

What you can also do is to use apt-get:

apt-get install python3-pip

In my experience this works pretty fluent too, plus you get all the benefits from apt-get.


回答 2

首先,使用以下命令安装Python 3 pip:

sudo apt-get install python3-pip

然后,Python 3 pip使用:

pip3 install <module-name>

对于Python 2 pip使用:

pip install <module-name>

First, install Python 3 pip using:

sudo apt-get install python3-pip

Then, to use Python 3 pip use:

pip3 install <module-name>

For Python 2 pip use:

pip install <module-name>

回答 3

如果您不想每次使用pip时都指定版本:

安装点子:

$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3

并导出路径:

$ export PATH=/Library/Frameworks/Python.framework/Versions/<version number>/bin:$PATH

If you don’t want to have to specify the version every time you use pip:

Install pip:

$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3

and export the path:

$ export PATH=/Library/Frameworks/Python.framework/Versions/<version number>/bin:$PATH

回答 4

最短的方法:

python3 -m pip install package
python -m pip install package

The shortest way:

python3 -m pip install package
python -m pip install package

回答 5

这在OS X上对我有用:(我之所以这样说,是因为有时Mac对于每个开放源代码工具都拥有“自己的”版本,这是一种痛苦,并且您无法删除它,因为“其改进”使它对于其他苹果产品来说都是独一无二的,如果您将其删除,事情就会开始恶化)

我按照@Lennart Regebro提供的步骤来获取python 3的pip,但是python 2的pip仍然是第一个出现的路径,所以…我要做的是在/ usr / bin内创建指向python 3的符号链接(实际上,我也做了同样的事情来让我的2条python和平运行):

ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/pip /usr/bin/pip3

请注意,我3在末尾添加了一个,因此基本上您要做的是使用pip3而不是pip

该帖子很旧,但我希望有一天能对某人有所帮助。从理论上讲,这应该适用于任何LINUX系统。

This worked for me on OS X: (I say this because sometimes is a pain that mac has “its own” version of every open source tool, and you cannot remove it because “its improvements” make it unique for other apple stuff to work, and if you remove it things start falling appart)

I followed the steps provided by @Lennart Regebro to get pip for python 3, nevertheless pip for python 2 was still first on the path, so… what I did is to create a symbolic link to python 3 inside /usr/bin (in deed I did the same to have my 2 pythons running in peace):

ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/pip /usr/bin/pip3

Notice that I added a 3 at the end, so basically what you have to do is to use pip3 instead of just pip.

The post is old but I hope this helps someone someday. this should theoretically work for any LINUX system.


回答 6

在Suse Linux 13.2上,pip调用python3,但是pip2可用于使用旧版本的python。

On Suse Linux 13.2, pip calls python3, but pip2 is available to use the older python version.


回答 7

在Windows中,先安装Python 3.7,然后再安装Python 2.7。然后,使用命令提示符:

pip安装python2-module-name

pip3安装python3-module-name

就这样

In Windows, first installed Python 3.7 and then Python 2.7. Then, use command prompt:

pip install python2-module-name

pip3 install python3-module-name

That’s all


回答 8

请注意,在msys2上,我发现以下命令会有所帮助:

$ pacman -S python3-pip
$ pip3 install --upgrade pip
$ pip3 install --user package_name

Please note that on msys2 I’ve found these commands to be helpful:

$ pacman -S python3-pip
$ pip3 install --upgrade pip
$ pip3 install --user package_name

回答 9

以为这是个老问题,我想我有更好的解决方案

  1. 要将pip用于python 2.x环境,请使用以下命令-

    py -2 -m pip安装-r requirements.txt

  2. 要将pip用于python 3.x环境,请使用以下命令-

    py -3 -m pip install -r requirements.txt

Thought this is old question, I think I have better solution

  1. To use pip for a python 2.x environment, use this command –

    py -2 -m pip install -r requirements.txt

  2. To use pip for python 3.x environment, use this command –

    py -3 -m pip install -r requirements.txt


如何在Python中将字符串转换为utf-8

问题:如何在Python中将字符串转换为utf-8

我有一个将utf-8字符发送到我的Python服务器的浏览器,但是当我从查询字符串中检索到它时,Python返回的编码是ASCII。如何将纯字符串转换为utf-8?

注意:从网络传递的字符串已经是UTF-8编码的,我只想让Python将其视为UTF-8而不是ASCII。

I have a browser which sends utf-8 characters to my Python server, but when I retrieve it from the query string, the encoding that Python returns is ASCII. How can I convert the plain string to utf-8?

NOTE: The string passed from the web is already UTF-8 encoded, I just want to make Python to treat it as UTF-8 not ASCII.


回答 0

>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)

^这是字节字符串(plain_string)和unicode字符串之间的差异。

>>> s = "Hello!"
>>> u = unicode(s, "utf-8")

^转换为unicode并指定编码。

In Python 2

>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)

^ This is the difference between a byte string (plain_string) and a unicode string.

>>> s = "Hello!"
>>> u = unicode(s, "utf-8")

^ Converting to unicode and specifying the encoding.

In Python 3

All strings are unicode. The unicode function does not exist anymore. See answer from @Noumenon


回答 1

如果上述方法不起作用,您还可以告诉Python忽略无法转换为utf-8的字符串部分:

stringnamehere.decode('utf-8', 'ignore')

If the methods above don’t work, you can also tell Python to ignore portions of a string that it can’t convert to utf-8:

stringnamehere.decode('utf-8', 'ignore')

回答 2

可能有些矫kill过正,但是当我在同一文件中使用ascii和unicode时,重复解码可能会很痛苦,这就是我使用的方法:

def make_unicode(input):
    if type(input) != unicode:
        input =  input.decode('utf-8')
    return input

Might be a bit overkill, but when I work with ascii and unicode in same files, repeating decode can be a pain, this is what I use:

def make_unicode(input):
    if type(input) != unicode:
        input =  input.decode('utf-8')
    return input

回答 3

将以下行添加到.py文件的顶部:

# -*- coding: utf-8 -*-

允许您直接在脚本中编码字符串,如下所示:

utfstr = "ボールト"

Adding the following line to the top of your .py file:

# -*- coding: utf-8 -*-

allows you to encode strings directly in your script, like this:

utfstr = "ボールト"

回答 4

如果我理解正确,则您的代码中包含utf-8编码的字节字符串。

将字节字符串转换为unicode字符串称为解码(unicode->字节字符串正在编码)。

您可以通过使用unicode函数或解码方法来实现。要么:

unicodestr = unicode(bytestr, encoding)
unicodestr = unicode(bytestr, "utf-8")

要么:

unicodestr = bytestr.decode(encoding)
unicodestr = bytestr.decode("utf-8")

If I understand you correctly, you have a utf-8 encoded byte-string in your code.

Converting a byte-string to a unicode string is known as decoding (unicode -> byte-string is encoding).

You do that by using the unicode function or the decode method. Either:

unicodestr = unicode(bytestr, encoding)
unicodestr = unicode(bytestr, "utf-8")

Or:

unicodestr = bytestr.decode(encoding)
unicodestr = bytestr.decode("utf-8")

回答 5

city = 'Ribeir\xc3\xa3o Preto'
print city.decode('cp1252').encode('utf-8')
city = 'Ribeir\xc3\xa3o Preto'
print city.decode('cp1252').encode('utf-8')

回答 6

在Python 3.6中,它们没有内置的unicode()方法。默认情况下,字符串已经存储为unicode,并且不需要转换。例:

my_str = "\u221a25"
print(my_str)
>>> 25

In Python 3.6, they do not have a built-in unicode() method. Strings are already stored as unicode by default and no conversion is required. Example:

my_str = "\u221a25"
print(my_str)
>>> √25

回答 7

使用ord()和unichar()进行翻译。每个Unicode字符都有一个关联的数字,如索引。因此,Python有一些方法可以在char和他的数字之间进行转换。缺点是一个例子。希望能有所帮助。

>>> C = 'ñ'
>>> U = C.decode('utf8')
>>> U
u'\xf1'
>>> ord(U)
241
>>> unichr(241)
u'\xf1'
>>> print unichr(241).encode('utf8')
ñ

Translate with ord() and unichar(). Every unicode char have a number asociated, something like an index. So Python have a few methods to translate between a char and his number. Downside is a ñ example. Hope it can help.

>>> C = 'ñ'
>>> U = C.decode('utf8')
>>> U
u'\xf1'
>>> ord(U)
241
>>> unichr(241)
u'\xf1'
>>> print unichr(241).encode('utf8')
ñ

回答 8

是的,您可以添加

# -*- coding: utf-8 -*-

在源代码的第一行中。

您可以在这里阅读更多详细信息https://www.python.org/dev/peps/pep-0263/

Yes, You can add

# -*- coding: utf-8 -*-

in your source code’s first line.

You can read more details here https://www.python.org/dev/peps/pep-0263/