String Pattern on a bytes-like object - python-3.x

Trying to move my application to Python 3.4...
My code:
def Continue(self):
exec(open(b"file.py").read())
My error:
TypeError: can't use a string pattern on a bytes-like object
My (miss)understanding is that by prefixing the filename string with a 'b' I am turning it into the required bytes like object.
Can anyone elighten me?

What is an alternative to execfile in Python 3?
Basically open the file, read it, compile the code, and run it.
with open("somefile.py") as f:
code = compile(f.read(), "somefile.py", 'exec')
exec(code, global_vars, local_vars)

Related

Python 3 upgrade, a bytes-like object is required, not 'str'

I've been trying to get this work but getting the error TypeError: a bytes-like object is required, not 'str'' after upgrading to python 3.
What am I doing wrong here? I tried r, wb+ and w learned from here, Confused by python file mode "w+"
my code:
with open(output_filename, 'wb') as f:
# write column names
f.write("stack,overflow,super,user\n")
writer = csv.writer(f)
Can anyone help with this? Thanks.
The difference between 'wb' and 'w' filemodes is that 'wb' directly reads the binary and 'w' reads it as string. Your issue is that you're using 'wb' instead of 'w'. csv.writer is expecting a string, not binary.
If you use with open(output_filename, 'w') as f: instead, it should work.

Python3 open text file

I am trying to open a simple text file using
dir_path = os.path.dirname(os.path.realpath(__file__))
F = open(dir_path+"\\sankey2.txt","r")
The path to the file is :C:\Users\David\Google Drive\Jobs\1.Efe\Transporte\0.MODEL
Inside the text file is the following text :
sankey2.txt
$SETGLOBAL REFERENCIA
Now if I run python it gives me the error :
TypeError: decoding to str: need a bytes-like object, _io.TextIOWrapper found
So I tried the following 2 solutions :
Trying this
F = bytes(open(dir_path+"\\sankey2.txt","r"))
I get
TypeError: 'str' object cannot be interpreted as an integer
Trying this
F = open(dir_path+"\\sankey2.txt","rb")
I get
TypeError: decoding to str: need a bytes-like object, _io.BufferedReader found
It is not entirely clear to me how to solve this problem. Encoding to UTF-8 doesn't help either.
Thanks for your help.

Python: When I try to run a code with (line +=) it gives a Typeerror

I am fairly new to python so mostly everything I've tried so far has failed.
Whenever I run this snip of my code to try to make a single string from a sequence,
with open('C:/Users/Arda Turan/Desktop/sequence.txt') as file:
lines = []
for line in file:
line+=(line.rstrip().split(","))
It gives the following error:
"C:\Users\Arda Turan\PycharmProjects\Untitled\venv\Scripts\python.exe"
"C:/Users/Arda Turan/.PyCharmCE2018.1/config/scratches/scratch_1.py"
Traceback (most recent call last):
File "C:/Users/Arda Turan/.PyCharmCE2018.1/config/scratches/scratch_1.py",
line 4, in <module>
line+=(line.rstrip().split(","))
TypeError: must be str, not list
Process finished with exit code 1
Any help would be appreciated.
I suppose, you meant:
for line in file:
lines.append(line.rstrip().split(","))
yes: your lines is an array, try with lines.append()
you're now using "lines" as if it is a string, while you have to use it as array, so use lines.append() instead of +=
if you want to do a "longer" string, change the declaration of lines with lines = '' and use the string catenation += so you'll have a longer string instead of an array of strings
You need to use list.append to append an element into the list. Try the following:
lines = []
with open('C:/Users/Arda Turan/Desktop/sequence.txt') as file:
for line in file:
lines.append(line.rstrip().split(","))

How to visualize Pyfst transducers via dot files

I am learning how to create transducers with Pyfst and I am trying to visualize the ones I create. The ultimate goal is to be able to write the transducers to dot files and see them in Graphviz.
I took a sample code to see how to visualize the following acceptor.
a = fst.Acceptor()
a.add_arc(0, 1, 'x', 0.1)
a[1].final = -1
a.draw()
When I use draw(), which comes with the package, I get an error:
File "/Users/.../tests.py", line 42, in <module>
a.draw()
File "_fst.pyx", line 816, in fst._fst.StdVectorFst.draw
(fst/_fst.cpp:15487)
File "/Users/.../venv-3.6/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
If I try to write the above mentioned acceptor to .dot via this:
def fst_dot(dot_object, filename):
path, file = split(filename)
new_path = join(dot_files_folder_path, path)
if not os.path.exists(new_path):
os.makedirs(new_path)
if hasattr(dot_object, 'dotFormat'):
draw_string = dot_object.dotFormat()
else:
draw_string = dot_object.draw()
open(join(dot_files_folder_path, filename + ".dot"), "w").write(draw_string)
then also I get the following error:
File "/Users/...tests.py", line 43, in <module>
fst_dot(a, 'acceptor')
File "/Users/...tests.py", line 22, in fst_dot
draw_string = dot_object.draw()
File "_fst.pyx", line 816, in fst._fst.StdVectorFst.draw
(fst/_fst.cpp:15487)
File "/Users/.../venv-3.6/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
So, both errors look the same - there is some kind of an issue with draw().
On the pyfst site it says that draw is used for dot format representation of the transducer.
I can't understand how to fix the error. Any help would be greatly appreciated.
I am using OSX and PyCharm.
You might try using Python2 to see if that helps at all.
However, I think you'll be better off using the Python bindings that are included with OpenFST 1.5+. That library also has the ability to write to GraphViz .dot files. There is documentation available here:
http://www.openfst.org/twiki/bin/view/FST/PythonExtension
I recommend you fstdraw command from openfst.
after a.write('tmp.fst') in python.
$ fstdraw tmp.fst > tmp.dot in shell.
EDIT:
Finally, I found that UFAL's forked pyfst works fine with python3.
https://github.com/UFAL-DSG/pyfst

How does one add string to tarfile in Python3

I have problem adding an str to a tar arhive in python. In python 2 I used such method:
fname = "archive_name"
params_src = "some arbitrarty string to be added to the archive"
params_sio = io.StringIO(params_src)
archive = tarfile.open(fname+".tgz", "w:gz")
tarinfo = tarfile.TarInfo(name="params")
tarinfo.size = len(params_src)
archive.addfile(tarinfo, params_sio)
Its essentially the same what can be found in this here.
It worked well. However, going to python 3 it broke and results with the following error:
File "./translate_report.py", line 67, in <module>
main()
File "./translate_report.py", line 48, in main
archive.addfile(tarinfo, params_sio)
File "/usr/lib/python3.2/tarfile.py", line 2111, in addfile
copyfileobj(fileobj, self.fileobj, tarinfo.size)
File "/usr/lib/python3.2/tarfile.py", line 276, in copyfileobj
dst.write(buf)
File "/usr/lib/python3.2/gzip.py", line 317, in write
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
TypeError: 'str' does not support the buffer interface
To be honest I have trouble understanding where it comes from since I do not feed any str to tarfile module back to the point where I do construct StringIO object.
I know the meanings of StringIO and str, bytes and such changed a bit from python 2 to 3 but I do not see a mistake and cannot come up with better logic to solve this task.
I create StringIO object precisely to provide buffer methods around the string I want to add to the archive. Yet it strikes me that some str does not provide it. On top of it the exception is raised around lines that seem to be responsible for checksum calculations.
Can some one please explain what I am miss-understanding or at least give an example how to add a simple str to the tar archive with out creating an intermediate file on the file-system.
When writing to a file, you need to encode your unicode data to bytes explicitly; StringIO objects do not do this for you, it's a text memory file. Use io.BytesIO() instead and encode:
params_sio = io.BytesIO(params_src.encode('utf8'))
Adjust your encoding to your data, of course.

Resources