2014-03-27 97 views
-1

我希望將文本文件的內容用作我的python腳本的輸入。這是我的劇本。我想替換幾句話:將文本文件的內容作爲python腳本的輸入

import subprocess 
import fileinput 
import sys 
import os 

for line in text: 
    line = line.replace("/dev/sda3    ", "") 
    line = line.replace("/dev/sda6    ", "") 
    line = line.replace("/dev/sda2    ", "") 
    line = line.replace("/dev/sda1    ", "") 
    line = line.replace("tmpfs     ", "") 

任何建議,非常感謝。

+0

...所以什麼是你的問題? – DBedrenko

回答

1
with open ("data.txt", "r") as myfile: 
    data=myfile.read().replace('\n', '') 

看看How do I read a text file into a string variable in Python

+0

這將取代每個實例的單詞? –

+0

我不確定你想要什麼,但它將文本文件導入到字符串中,同時替換它的一部分(在本例中爲換行符(\ n)),它將替換爲不在文件中的字符串,如您所見從'r'-option(read)中打開命令,如果你想這樣做幾次,比做幾次。 – theaembee

相關問題