अजगर स्क्रिप्ट आप sys.stdin से पढ़ किया जाना चाहिए के अंदर:
import sys
INPUT = sys.stdin
def do_something_with_data(line):
# Do your magic here
...
return result
def main():
for line in INPUT:
print 'Result:', do_something_with_data(line)
if __name__ == '__main__':
main()
पुनरावृत्ति दुभाषिया के अंदर आप subprocess मॉड्यूल नकली sys.stdin उपयोग कर सकते हैं।
In[0]: from test.py import *
In[1]: INPUT = subprocess.Popen(['grep', 'ABC', 'input.txt'], \
stdout=subprocess.PIPE).stdout
In[2]: main()
आप आउटपुट को फ़ाइल में पाइप भी कर सकते हैं और फ़ाइल से पढ़ सकते हैं। व्यावहारिक उद्देश्यों के लिए stdin सिर्फ एक और फाइल है।
In[0]: ! grep "ABC" input.txt > output.txt
In[1]: INPUT = open('output.txt')
In[2]: main()
स्रोत
2012-05-19 02:37:45
आईपीथॉन से परिचित नहीं है। किसी भी कारण से आप sys.stdin या fileinput मॉड्यूल का उपयोग नहीं कर सकते हैं? – Triptych
@Triptych मैं grep द्वारा जेनरेट किए गए उस विशेष इनपुट के साथ डीबग करना चाहता हूं। – CodeNoob
[fileinput मॉड्यूल] (http://docs.python.org/library/fileinput.html) का उपयोग करने का प्रयास करें और देखें कि यह कैसा चल रहा है। लेकिन वास्तव में इस सवाल का जवाब देने के लिए: हाँ, आप इसे कर सकते हैं लेकिन यह इस बात पर निर्भर करता है कि पाइथन कैसे लिखा जाता है। – Keith