reading a comma separtated text file containing special characters in python - python

import csv,sys
phile="E:/Users/san/Documents/phonebook.txt"
ph=open(phile,"rt")
try:
lines= csv.reader(ph)
for each in lines:
print each,
except Exception as er:
print er
finally: ph.close()
getting error saying that " line contains NULL byte" !!

My other guess is that the open(phile, "rt") should just be open(phile, "rb").
You can also debug which line number this happens on (or one before) by doing this:
for line_number, each in enumerate(lines):
print line_number, each

Related

Python NameError exception not working as intended

When I run this code, a NameError traceback error pops up, even though it should be handled by the exception. Why is that?
The function call argument is intentionally misspelled.
filename_cats = "cats.txt"
filename_dogs = "dogs.txt"
def readlines(filename):
"""read lines from a text file"""
try:
with open(filename) as f:
lines = f.readlines()
string = ''
for line in lines:
string += line
except (NameError, FileNotFoundError):
print(f"The file {filename} was not found.")
else:
print(string)
readlines(filename_cat)
It's because the error happens here:
👇
readlines(filename_cat) 👈
☝️
Not anywhere in here:
try:
with open(filename) as f:
lines = f.readlines()
string = ''
for line in lines:
string += line
except (NameError, FileNotFoundError):
A try..except block can only catch errors happening literally within it, not anything happening before or after it.

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))

Read an input file in 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

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

"_csv.Error: line contains NULL byte" in CSV reader from STDIN

There are many StackOverflow questions about this error when reading from a CSV file. My problem is occurring while reading from STDIN.
[Most SO solutions talk about tweaking the open() command which works for opening CSV files - not for reading them through STDIN]. My problem is with reading through STDIN. So please don't mark this as a duplicate.
My python code is:
import sys , csv
def main(argv):
reader = csv.reader(sys.stdin, delimiter=',')
for line in reader:
print line
and the returned error is:
Traceback (most recent call last):
File "mapper.py", line 19, in <module>
main(sys.argv)
File "mapper.py", line 4, in main
for line in reader:
_csv.Error: line contains NULL byte
It would suffice me to simply ignore that line where the NULL byte occurs (if that is possible) in the for loop.
i solved it by handling CSV exception
import sys , csv
def main(argv):
reader = csv.reader(sys.stdin, delimiter=',')
lineCount = 0
errorCount = 0
while True:
# keep iterating indefinitely until exception is raised for end of the reader (an iterator)
try:
lineCount += 1
line = next(reader)
print "%d - %s" % (lineCount , line)
except csv.Error:
# this exception is raised when a malformed CSV is encountered... ignore it and continue
errorCount += 1
continue
except StopIteration:
# this exception is raised when next() reaches the end of the iterator
lineCount -= 1
break
print "total line: %d" % lineCount
print "total error: %d" % errorCount

Categories