Read an input file in python - python

I have an input file which looks like
===========================
__A= 2.3
__b= 3.26
===========================
__ctx= 4.2
===========================
__itermax= 100
__errmax= 1e-07
Using this inputfile a collection of outputs within a different code( not implemented in python) has been generated. I am now trying to write some Python scripts to read this input file as well as other generated outputs to perform postproccessing analysis.
I thus would like to extract the value of each parameter(A, b, ctx, ...) by a python script. Please note that this input file is distinct from the setting file discussed here as I can not perform any modification on the structure of the input file.
I have tried
import sys
try:
directory_name=sys.argv[1]
print(directory_name)
except:
print('Please pass directory_name')
input_file = directory_name +"input.txt"
with open(input_file) as fin:
for line in fin:
exec(line)
The error that I am encountering after running the code is
File "code.py", line 14, in <module>
exec(line)
File "<string>", line 1
===========================
^
SyntaxError: invalid syntax
Any suggestion on improving this code, or with a different outputting method (e.g. as a dictionary), to obtain the values of the parameters is helpful.

Do you wanna exec the string "==================" ?
This string is not a python code.
There is a lazy way, use try ... except ... to resolve this.
import sys
try:
directory_name=sys.argv[1]
print(directory_name)
except:
print('Please pass directory_name')
input_file = directory_name +"input.txt"
with open(input_file) as fin:
for line in fin:
try:
exec(line)
except Exception as e:
print(e)
Another way is you can remove all unuseful strings before you exec them.

Try a simple regular expression:
import re
e = r'^__(.*?)=(.*?)$'
with open('data.txt') as f:
print(dict(re.findall(e, f.read(), re.M)))
This will print:
{'A': ' 2.3', 'b': ' 3.26', 'ctx': ' 4.2', 'itermax': ' 100', 'errmax': ' 1e-07'}

Executing random lines of code from a file is risky, and a bit 'hacky'. If you really want to do it, the easiest way to fix your code is to just try each line:
import sys
try:
directory_name=sys.argv[1]
print(directory_name)
except:
print('Please pass directory_name')
input_file = directory_name +"input.txt"
with open(input_file) as fin:
for line in fin:
try:
exec(line)
except:
print("Line invalid: {}".format(line))

for extract thei value use re.search
import re
import sys
textfile = sys.argv[1]
f = open(textfile, 'r').readlines()
for l in f:
extract = l.rstrip()
try:
f = re.search('__A= (.*)', extract)
return True
except:
return False
valueA = f.group(1)
print valueA

you first have to check "line" makes sens to be executed.
the problem is when line = "========="
you can use :
if line.startwith("===")
continue
to skip it.
or
if line.startwith("__"):
exec(line)
to avoid exectuting unknown stuff

Related

Check if a variable string exist in a text file

So guys, i'm tryng to make a password generator but i'm having this trouble:
First, the code i use for tests:
idTest= "TEST"
passwrd= str(random.randint(11, 99))
if not os.path.exists('Senhas.txt'):
txtFileW = open('Senhas.txt', 'w')
txtFileW.writelines(f'{idTest}: {passwrd}\n')
txtFileW.close()
else:
txtFileA = open('Senhas.txt', 'a')
txtFileA.write(f'{idTest}: {passwrd}\n')
txtFileA.close()
print(f'{idTest}: {passwrd}')
Well, what i'm expecting is something like this:
else:
with open('Senhas.txt', 'r+') as opened:
opened.read()
for lines in opened:
if something == idTest:
lines.replace(f'{something}', f'{idTest}')
else:
break
txtFileA = open('Senhas.txt', 'a')
txtFileA.write(f'{idTest}: {passwrd}\n')
txtFileA.close()
print(f'{idTest}: {passwrd}')
I've searched for it but all i've found are ways to separate it in 2 files (for my project it doesn't match) or with "static" strings, that doesn't match for me as well.
You can use the fileinput module to update the file in place.
import fileinput
with fileinput.input(files=('Senhas.txt'), inplace=True) as f:
for line in f:
if (line.startswith(idTest+':'):
print(f'{idTest}: {passwrd}')
else:
print(line)

Replace multiple Strings in a file using fileinput

I am trying to replace multiple strings in a file.
My file may have some contents like this:
file1:
#groovy
some test
some more test
REPLACE_1
REPLACE_OPTIONAL_1
REPLACE_2
end test
I am trying to use fileinput module to replace above text but its not working as expected. My method is something like this:
import fileinput
def replace_method():
file_path = './file1.txt'
try:
with fileinput.FileInput(file_path, inplace=True, backup=".bak") as file:
for line in file:
print (line.replace('REPLACE_1', 'replaced_value1'), end='')
print (line.replace('REPLACE_OPTIONAL_1', 'replaced_value2'), end='')
print (line.replace('REPLACE_OPTIONAL_2', 'replaced_value3'), end='')
print (line.replace('REPLACE_2', 'replaced_value4'), end='')
except Exception as e:
print (str(e))
Above code works but it prints everyline 4 times in the new modified file. I believe this is something to do with line.replace which Imight be using wrongly.
Can you please help me fixing this.
Please let me know if you need more information.
Don't print 4 times
import fileinput
def replace_method():
file_path = './file1.txt'
try:
with fileinput.FileInput(file_path, inplace=True, backup=".bak") as file:
for line in file:
line = line.replace('REPLACE_1', 'replaced_value1')
line = line.replace('REPLACE_OPTIONAL_1', 'replaced_value2')
line = line.replace('REPLACE_OPTIONAL_2', 'replaced_value3')
line = line.replace('REPLACE_2', 'replaced_value4')
print (line, end='')
except Exception as e:
print (str(e))
The problem is that you are printing the line four times instead of one. The fix could look like:
import fileinput
def replace_method():
file_path = './file1.txt'
try:
with fileinput.FileInput(file_path, inplace=True, backup=".bak") as file:
for line in file:
line.replace('REPLACE_1', 'replaced_value1')
line.replace('REPLACE_OPTIONAL_1', 'replaced_value2')
line.replace('REPLACE_OPTIONAL_2', 'replaced_value3')
line.replace('REPLACE_2', 'replaced_value4')
print(line, end='')
except Exception as e:
print (str(e))

execfile issue with the filename argument in python

I have two python scripts. One match script, the other is regex script. I want to start with my matchParser.py script and then go to regexParser.py. I want that my regexParser knows the filename of the matchParser and continue to use. I hope I could explain it clearly. I tried a lot, but unfortunately without success.
My OutputError: TypeError: coercing to Unicode: need string or buffer, file found
matchParser.py
import glob
intfiles = glob.glob("C:\Users\x\Desktop\x\*.csv")
header_saved = False
with open('outputdre121.csv','wb') as fout:
for filename in intfiles:
with open(filename) as fin:
header = next(fin)
if not header_saved:
fout.write(header)
header_saved = True
for line in fin:
fout.write(line)
print "start next script: regexParser.py"
execfile("regexParser.py")
regexParser.py
import re
import matchParser
lines = [line.strip() for line in open(matchParser.fout)] ## here the filename from MatchParser.py
with open('outputregexdre13.csv', "w") as output_csv:
for result in lines:
match = re.search(r'((\s\d+?[1-9])!?[ ])', result)
if match: output_csv.write(match.group(0) + '\n')
thanks!
I have found the solution:
Very simple actually ..
I add to matchParser.fout the .name method
lines = [line.strip() for line in open(matchParser.fout.name)]

How to print a text file in python

I'm fairly new to coding and am having some issues printing a text file.
Here's my file:
Player1: 1
Player2: 3
Here's my code:
try:
scoreTable = open("scoreTable.txt", "r")
line = scoreTable.readlines()
for i in range(0, (len(line))):
print(scoreTable.read(len(line[i].strip("\n"))))
scoreTable.close()
except FileNotFoundError:
pass
At the moment its just printing whitespace.
I'm probably missing something obvious or have gone down the wrong road altogether, so any help would be appreciated.
Thanks in advance.
Just use the below code sample to print the whole file.
try:
with open("scoreTable.txt", "r" ) as scoreTable:
file_content = scoreTable.read()
print str(file_content)
except FileNotFoundError as e:
print e.message
You are performing read operation on scoreTable.txt twice, which is not required.
try:
scoreTable = open("scoreTable.txt", "r")
lines = scoreTable.readlines()
#here in lines you have whole file stored so no need to try to read from files variable again
for line in lines:
print line
scoreTable.close()
except FileNotFoundError:
pass
While we are on this subject use with statement to read files(so you wont have to keep track to close the file)
with open("scoreTable.txt", "r" ) as f:
lines = f.readlines()
for line in lines:
print line

Write a line above the current line in a file Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am looping through a file and I need to insert a text block above a line when it matches a particular string. Please help!
convBlockMoved = False
for line in outfile:
if(line.startswith('mappingSchemeAxis') and not convBlockMoved):
for convLine in conversionBlocks:
print convLine, #Print this above line
convBlockMoved = True
Note: conversionBlocks is a String array
Not a Python answer, but sed can do this in one line.
The file:
$ cat > so.txt
foo
bar
baz
qux
Insert before line baz:
$ sed -i '/baz/i11\n22\n33' so.txt
The result:
$ cat so.txt
foo
bar
11
22
33
baz
qux
So if your file is not huge, you can read all the lines at once and then work with lists. An example using the insert method of lists would be:
def main():
lines = []
with open('input.txt') as f:
lines = f.readlines()
ins_at = find_occurences_of('mappingSchemeAxis', lines)
for i in ins_at:
lines.insert(i,'HELLO WORLD\n')
with open('input.txt', 'w') as f:
f.writelines(lines)
def find_occurences_of(needle, haystack):
ret = []
for i, line in enumerate(haystack):
if line.startswith(needle):
ret.append(i)
return ret
if __name__ == '__main__':
main()
Basically, you are reading a list of strings and you want to put a new list element above the current one under some conditions.
What I would suggest you (if the file is not too big) is to append the lines from your input to an output list, appending the text you want before each lines that matche your conditions. Something like the following
for line in infile.readlines ():
if line.startswith ('mappingSchemeAxis'):
outcontent.append ('xxxxx')
outcontent.append (line)
for line in outcontent:
print (line) # here you want to write the content to the output file
I posted that a bit late :D
Try this:
def replace_import_in_f(f_in, pattern, plus):
with open(f_in) as f:
in_str = f.read()
in_str = re.sub(pattern, pattern + plus + "\n", in_str)
with open(f_in, "w") as f:
f.write(in_str)
Pattern must be the entire line that you would add the new line above.
Note: This is perfect for medium file due to the f.write() of the whole file content. (Test with python 3.4)
[UPDATE]
More complicated but to handle big files, use of coroutine to write to temp file during line processing. If not error replace the temp file.
import tempfile, os
def write_file(pattern="", plus=""):
with tempfile.NamedTemporaryFile(delete=False) as fin:
yield fin
while True:
line = (yield)
if pattern:
if line.startswith(pattern):
fin.write(bytes(plus, 'UTF-8'))
fin.write(bytes(line, 'UTF-8'))
def copy_file(path_in, path_out):
with open(path_in) as fin, open(path_out, "w") as fout:
for line in fin:
fout.write(line)
def read_and_file(fname, pattern="", plus=""):
try:
with open(fname) as fh:
# generator creation
gen_write = write_file(pattern, plus)
# get the tempfile
fout = next(gen_write)
for line in fh:
# send line
gen_write.send(line)
except (IOError, OSError) as e:
print(e)
else:
fout.close()
if os.name == "nt":
copy_file(fout.name, fname)
else:
os.rename(fout.name, fname)

Categories