小编Moh*_*hit的帖子

从python中的dateutil.parser.parse获取时区

尝试解析带有时区信息的datetime字符串并获取utc偏移量

from dateutil.parser import parse as parse_date
s = '2017-08-28 06:08:20,488 CDT'
dt =  parse_date(s)
print dt.utcoffset()
None
Run Code Online (Sandbox Code Playgroud)

为什么utcoffset返回None而不是-5作为偏移量?

python python-dateutil

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

Dockerfile wget 失败

我有以下代码

RUN apt-get update
RUN apt-get install -y wget     #install wget lib
RUN mkdir -p example && cd example     #create folder and cd to folder
RUN WGET -r https://host/file.tar && tar -xvf *.tar   # download tar file to example folder and untar it in same folder
RUN rm -r example/*.tar # remove the tar file
RUN MV example/foo example/bar # rename untar directory from foo to bar
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

/bin/sh: 1: WGET: not found
tar: example/*.tar: Cannot open: No such file …
Run Code Online (Sandbox Code Playgroud)

docker dockerfile

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

改变(例如)8到08 ... python

我正在读取csv文件中的数据,其中有日期元素,但日期不一致.

例如:有时日期元素就像1/1/2011,有时它就像01/01/2011

由于我以后正在绘制这些数据...这会在我的情节中引起很大的噪音.以下是我的日期课程.你能帮我解决在哪里以及如何修改代码以获取表单中的日期01/01/2011

import re
class Date:
def __init__(self, input_date):
    self._input_date = input_date
    self._date = None
    self._month = None
    self._year = None
    self._hour = None
    self._min = None


def  setDate(self):
    date = self._input_date
    #date = re.findall('w+',date)
    date = self.__mySplit()
    #print"len ",len(date)
    assert (len(date) >= 3) #has atleast dd/mm/yy 
    #dateLength = len(date[0])

    self._month = int(date[0])
    self._date = int(date[1])
    self._year = int(date[2])
    if (len(date) ==5):
        self._hour = int(date[3])
        self._min = int(date[4])

def __mySplit(self): #splitting the date by delimiters.. …
Run Code Online (Sandbox Code Playgroud)

python algorithm date file

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

从php调用python

可能重复:
从PHP调用Python并获取返回代码

可能是一个非常愚蠢的问题我有一个python模块让我们说hi.py有以下内容

print "hi"
Run Code Online (Sandbox Code Playgroud)

现在我想从php执行这个文件

<?php

#code here
?>
Run Code Online (Sandbox Code Playgroud)

那么如何从php调用该python文件.谢谢

php python

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

从命令行python获取参数

我试图从命令行获取三个参数:

-o (for outputfile) -k (number of clusters) -l (data to be clustered)
Run Code Online (Sandbox Code Playgroud)

所以我写了这个.

def get_input():
print 'ARGV      :', sys.argv[1:]

options, remainder = getopt.getopt(sys.argv[1:], 'o:v:k:l', ['output=', 
                                                     'verbose',
                                                     'k_clust=',
                                                     'limit='])
print "options ",options
file_flag , k_flag, count_flag = False, False,False
for opt, arg in options:
    print opt
    if opt in ('-o', '--output'):
        print "here ", opt, arg
        output_filename = arg
        o_flag = True

    if opt in ('-v', '--verbose'):
        verbose = True
    if opt == '--version':
        version = arg

    if opt in …
Run Code Online (Sandbox Code Playgroud)

python

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

压缩列表的python dict

我有一个类型为defaultdict(list)的python字典这个字典是这样的:

a = {1:[1,2,3,4],2:[5,6,7,8]....n:[some 4 elements]}
Run Code Online (Sandbox Code Playgroud)

所以基本上它有n个键,它有一个列表作为值,所有列表都是相同的长度.现在,我想建立一个类似这样的列表.

[[1,5,...first element of all the list], [2,6.. second element of all the list]... and so on]
Run Code Online (Sandbox Code Playgroud)

所以我基本上如何从所有键获得kth值..是否有pythonic方式来做到这一点.. ?? 谢谢

python

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

从list中检索元素:python

可能是一个非常天真的问题:

我有一个清单:

color = ["red","blue","red","green","blue"]
Run Code Online (Sandbox Code Playgroud)

现在,我想遍历列表

for c in color:
   # color is "red"
     get the rest of the colors except this red
    so rest = ["blue","red","green",blue"]
Run Code Online (Sandbox Code Playgroud)

在下一次迭代中:

    c = blue
    rest = ["red","red","green","blue"]
Run Code Online (Sandbox Code Playgroud)

呃.为什么我有一种感觉,这是非常微不足道的..并且可能有一个单行命令可以解决这个问题?谢谢

python

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

在字符串python中找到一个模式

我有一个非常嘈杂的字符串输入,我正在尝试清理它..

所以,嘈杂字符串的一部分可能是这样的:

"big $price chair, 5x10"
Run Code Online (Sandbox Code Playgroud)

现在删除符号和其他东西!但我也想删除

  5x10
Run Code Online (Sandbox Code Playgroud)

为此我做了这个:

 def remove_numerics(self,string):
    return ' '.join([term for term in string.split() if not term[0].isdigit()])
Run Code Online (Sandbox Code Playgroud)

这解决了这个案子

但如果我的字符串是:

    "big $price chair, x10"
Run Code Online (Sandbox Code Playgroud)

然后它失败了?什么是一个很好的pythonic方式来解决这个案例.非常感谢.

python

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

为什么排序k1,1会改变输出?

我是hadoop世界的新手,我正在努力学习用地图减少思维方式编写代码.

所以,我正在关注michael-noll教程.

我面临的挑战之一(除了理解新框架)是这个框架使用的终端技巧的数量.

那是什么呢.

  $echo "foo foo quux labs foo bar quux" | /home/hduser/mapper.py | sort -k1,1 | /home/hduser/reducer.py
Run Code Online (Sandbox Code Playgroud)

手段???echo有什么作用?

此外,上述代码的输出是:

  bar     1
  foo     3
  labs    1
   quux    2
Run Code Online (Sandbox Code Playgroud)

现在,如果我没有排序-k1,1 thingy

  foo     2
  bar     1
  labs    1
  foo     1
   quux    2
Run Code Online (Sandbox Code Playgroud)

排序标志有什么影响?-k1,1是什么意思?

谢谢..

参考:http: //www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/

shell terminal hadoop

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

python中字典列表的两级排序

我有一个字典列表如下:

 {"id": 1, "score": some_score.. othe values}
 {"id": 1, "score": some_differetscore.. othe values}  
 {"id": 22, "score": some_score.. othe values}
 {"id": 3, "score": some_score.. othe values}
Run Code Online (Sandbox Code Playgroud)

我希望得到的是通过这样的方式迭代这个列表,它按如下方式排序.

列表按"id"排序,然后按"score"反向排序

因此,所有带有"id"1的条目都会聚集在一起,然后最高分的条目位于顶部?我该怎么做呢?

谢谢

python sorting

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