python - Call sed via subprocess -
python - Call sed via subprocess -
i need phone call sed via python. tried reply of this question, doesn't work.
my code (yes file doesn't have extension, works when using putty):
filename = '/home/user1/file' subprocess.call(["sed","-i",r"$'s/[][]//g;s/,/\\\n/g'",filename])
can see going wrong? script executes, skips subprocess call.
ok tried using python re module code:
filename = '/home/user/file' filenametemp = '/home/user/temp' file = open(filename, "r") temp = open(filenametemp,"w+") text = file.read() text = re.sub(r'\[|\]',r'',text) text = re.sub(r',',r'\n', text) print text temp.write(text) header_row=['h1','h2','h3'] info = pd.read_csv(filenametemp, sep= ';',skipinitialspace=1, names=header_row)
the file "temp" in right layout, pandas creates empty dataframe when opening file. if comment out:
#temp = open(filenametemp,"w+") #temp.write(text)
and run script 1 time again info loaded correctly in dataframe.
can explain why happening , has solution this?
it turned out had close file before can used. although not done subprocess phone call sed returned right solution:
filename = '/home/user/file' filenametemp = '/home/user/temp' file = open(filename, "r") temp = open(filenametemp,"w+") text = file.read() text = re.sub(r'\[|\]',r'',text) text = re.sub(r',',r'\n', text) print text temp.write(text) temp.close() header_row=['h1','h2','h3'] info = pd.read_csv(filenametemp, sep= ';',skipinitialspace=1, names=header_row)
1) "i need phone call sed via python". don't think need that. python program, using re
module, can sed
can do, , more.
2) "the script executes, skips subprocess call." don't think that's accurate, either. seems much more subprocess.call
is invoked, , sed
runs, syntax error in sed
command prevents sed
completing duty.
3) string had several characters in it. didn't need $
, '
, or quite many \
. seek this:
import subprocess filename = '/tmp/file' subprocess.call(["sed","-i",r"s/[][]//g;s/,/\n/g",filename])
here /tmp/file
before:
hello[] is[] test, test, you[] know?
and after:
class="lang-none prettyprint-override">hello test test know?
python sed
Comments
Post a Comment