小编PM *_*ing的帖子

python + 使用变量运行系统命令

我需要从 python 运行系统命令

我有 python - 版本 - Python 2.4.3

我尝试以下操作,在本例中为ls -ltr | 八月

#!/usr/bin/python


import commands


Month = "Aug"
status,output = commands.getstatusoutput(" ls -ltr | grep Month "  )
print output
Run Code Online (Sandbox Code Playgroud)

如何在命令中插入 Month 变量?

所以grep会这样做

  | grep Aug
Run Code Online (Sandbox Code Playgroud)

我也试试这个

status,output = commands.getstatusoutput( " ls -ltr | grep {} ".format(Month) )
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误

Traceback (most recent call last):
   File "./stamm.py", line 14, in ?
    status,output = commands.getstatusoutput( " ls -ltr | grep {}     ".format(Month) )
AttributeError: 'str' object has no …
Run Code Online (Sandbox Code Playgroud)

python linux python-2.4

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

python numpy将字符串转换为复数

我有一个字符串列表,每个字符串都是一个复数。格式如下:

lr=['3.123+2.43i','0.23454-1.2134i','0.6543+0.76543i','1.12324+0.6543i']
Run Code Online (Sandbox Code Playgroud)

我想要的是每个复数的模数列表。

python numpy complex-numbers

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

如何将文本文件中的单词放入python中的字典中

我需要将txt文件中的所有单词放入字典中:

例如,我有这样的 f.txt:

a tom
a sam
b stern 
c stern 
a king
Run Code Online (Sandbox Code Playgroud)

我希望得到:

{'a': 'tom', 'sam', 'king', 'b':'stern', 'c': 'stern'}    
Run Code Online (Sandbox Code Playgroud)

这是我的代码

new_dict = {}
myFile = open('f.txt', 'r')
for line in myFile:
    line2=line.split()
    group=line2[0]
    name=line2[1]
    new_dict[group]= name
new_dict
Run Code Online (Sandbox Code Playgroud)

这段代码有问题。输出不能很好地读取这个文件,我只得到了部分键和值,而不是全部。

例如:

我懂了:

{'a': 'tom', 'b':'stern'} 
Run Code Online (Sandbox Code Playgroud)

如何处理?

python dictionary

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

在 While 循环中打印字典

我一直在环顾四周,看看是否有人真的这样做过,但找不到,所以希望我能在这里得到一些帮助。

newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
Run Code Online (Sandbox Code Playgroud)

我创建了这个 dict,我想使用while循环以这种方式输出它:

Jan 31
Feb 29
Mar 31
Apr 30
May 31
Jun 30
Jul 31
Aug 30
Run Code Online (Sandbox Code Playgroud)

我可以用for循环来完成,只是好奇如何用while循环来完成。

python python-2.7

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