简体   繁体   English

相当于Bash $()的Python

[英]Python equivalent to Bash $()

I search the Python equivalent for the following Bash code: 我在Python等效项中搜索以下Bash代码:

VAR=$(echo $VAR)

Pseudo Python code could be: 伪Python代码可能是:

var = print var

Can you help? 你能帮我吗? :-) :-)

Regards 问候

Edit: 编辑:

I search a way to do this: 我寻找一种方法来做到这一点:

for dhIP in open('dh-ips.txt', 'r'):
    gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
    print gi.country_code_by_addr(print dhIP) # <-- this line is my problem

In Bash i would do it like this: 在Bash中,我会这样做:

print gi.country_code_by_addr($(dhIP)) # only pseudo code... 打印gi.country_code_by_addr($(dhIP))#仅伪代码...

Hope it's more clear now. 希望现在更加清楚。

Edit2: 编辑2:

Thank you all! 谢谢你们! Here's my solution which works. 这是我的解决方案。 Thanks to Liquid_Fire for the remark with the newline char and thanks to hop for his code! 感谢Liquid_Fire用换行符char所做的评论,也感谢Hop提供了他的代码!

import GeoIP

fp = open('dh-ips.txt', 'r')
gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)

try:
    for dhIP in fp:
        print gi.country_code_by_addr(dhIP.rstrip("\n"))
finally:
    fp.close()

You don't need a print in there, just use the name of the variable: 您不需要在那里print ,只需使用变量的名称即可:

for dhIP in open('dh-ips.txt', 'r'):
    gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
    print gi.country_code_by_addr(dhIP)

Also note that iterating through a file object gives you lines with the newline characters at the end. 还要注意,遍历文件对象使您的行末尾带有换行符。 You may want to use something like dhIP.rstrip("\\n") to remove them before passing it on to country_code_by_addr . 在将其传递给country_code_by_addr之前,您可能希望使用dhIP.rstrip("\\n")东西将其删除。

Just use dhIP as it is. dhIP原样使用dhIP There is no need to do anything special with it: 无需对其进行任何特殊处理:

for dhIP in open('dh-ips.txt', 'r'):
    gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
    print gi.country_code_by_addr(dhIP)

NB: There are some other issues with your code. 注意:您的代码还有其他一些问题。

Without being familiar with the library you use, it seems to me that you unnecessarily instantiate GeoIP in every iteration of the loop. 在不熟悉您使用的库的情况下,在我看来,您不必要在循环的每次迭代中实例化GeoIP。 Also, you should not throw away the file handle, so you can close the file afterwards. 同样,您不应该丢弃文件句柄,以便以后可以关闭文件。

fp = open('dh-ips.txt', 'r')
gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)

try:
    for dhIP in fp:
        print gi.country_code_by_addr(dhIP)
finally:
    fp.close()

Or, even better, in 2.5 and above you can use a context manager: 或者,甚至更好的是,在2.5及更高版本中,您可以使用上下文管理器:

with open('dh-ips.txt', 'r') as fp:
    gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
    for dhIP in fp:
        print gi.country_code_by_addr(dhIP)

You might want to try these functions: 您可能想尝试以下功能:

str(var) str(var)

repr(var) repr(var)

If you're just trying to reassign a value to the same name it would be: 如果您只是想将值重新分配为相同的名称,则为:

var = var

Now if you're trying to assign the string representation (which is usually what print returns) of whatever object is referred to by var : 现在,如果您要分配var所引用的任何对象的字符串表示形式(通常是print返回的内容):

var = str(var)

Is that what you're after? 那是你追求的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM