Reading from a file with printer commands and printing in Python3 - python-3.x

I'm reading a file and I'm trying to send it to a PCL printer HP LJ2035 with
os.startfile("C:/tmp/tmp.txt", "print")
That file which I read is encoded in IMB-852 from DOS and has printer commands inside like ^[E^[(17U^[(s10.2H or ^[(s3B.
My main issue is here that os.startfile prints from Windows on a default printer and it prints also printer commands and doesn't interpret them. The file prints fine when I send it from program writen in Clarion (in DOS) directly to LPT port without Windows printing.
In Linux command file on tmp.txt file encoded in IBM-852 returns:
tmp.txt: HP PCL printer data
How should I read and print that file in Python 3 so that my printer interprets printer commands? Do I need to open that file in binary mode? If I do with open(my_file, 'rb') as f: I get an error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x86 in position 62: invalid start byte

Related

File transfer via serial port

I am trying to send a file via serial port. I tried sending picocom commands in a bash script. However the device writes out a menu of options. When it gets to the file transfer step - it writes out 'C' until a file is sent, after which it does the handshake and proceeds with the transfer. With the script I am not able to send the keystrokes 'C-a' and 'C-s' to get the '*** file:' prompt of picocom. I can do it manually. I even tried a combination of bash and python and pyautogui for the keystrokes, bash echo command to send the hex version of the keystrokes. I even tried sending 'sz' commands through the script and stty. All of these attempts were unsuccessful.
So I switched to python, and tried the xmodem library in python. I am supposed to use ymodem. I thought the 'YMODEM batch transmission session' in the library would do the job. The modem.send command always errors out with the file not being sent.
Read Byte: b'C'
Put Byte: 133
Read Byte: b'C'
send error: expected ACK; got b'C' for block 1
Put Byte: 133
Read Byte: b'C'
send error: expected ACK; got b'C' for block 1
How can i get around this?
Finally ended up using a combination of python and bash - python for serial communication and for parsing the file names of the binaries, and, bash stty and sb commands for transferring the binaries.

How to print ダイスキ using Python 3.7 in Scite?

I'm using Win10 & Scite with utf-8 enabled output window.
The file is saved as UTF-8 with BOM
Script:
print('ダイスキ from python 3')
The script can be run on cmd prompt without error. But when run on Scite it will produce error:
Output:
>pythonw.exe -u "test.py"
Traceback (most recent call last):
File "test.py", line 12, in <module>
print('\u30c0\u30a4\u30b9\u30ad from python 3')
File "D:\BIN\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1-2: character maps to <undefined>
>Exit code: 1
How to properly print ダイスキ to stdout using python3 with Scite?
Updates:
I've edited Scite Global Options file, to support utf-8.
code.page=65001
I've tested C, Lua, old Python 2.7, and it can print utf-8 strings (on Scite output window).
Seems to be Scite configuration error or a maybe Scite bug, because the Scite output terminal window works on Lua & C, but fail only on Python3.
Scite involves popen() / piping the STDOUT.
Python 3.7 need env variable "PYTHONIOENCODING" to be set manually. So you need to add environment variable "PYTHONIOENCODING" set to "utf_8"
Result:
Try to do this:
print(u'ダイスキ')

Stop Python adding " " when reading some .csv files

I have a number of .csv files which I am opening in Python3. Some open fine and the script runs fine, others I get the below error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 5547: invalid start byte
If I tell Python to ignore errors as below
dataset = open('data.csv', 'r', errors='inore')
The script then runs but it adds quotation marks around each column header in the .csv e.g.
"No.","Time","Source","Destination"
How can I open the .csv without the quotation marks, as per the others that already do this e.g. below
No.,Time,Source,Destination
I have tried this running on Linux Mint 18.3 with Python 3.6.4 and Mac OSx with Python 3.6.3 and get same results on both. I do not have a windows PC to try.
try to strip the string mate :)
a ="\"a\""
print(a.strip("\""))
or replace the "
a.replace("\"", "")

Why is stdout printing out an empty string in this case?

I am reading about subprocess and playing around with some code.
I'm using Windows 7 with Python3.6
import subprocess
process = subprocess.Popen(['notepad', 'C:\\Users\Amit\Downloads\InsiderTrades.txt'],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#I'm opening a text file which has a list of stock tickers
stdout1, stderr1 = process.communicate()
print(stdout1.decode('ASCII'))
The output I'm getting is nothing or
b'' as the value for stdout1.
I"m not quite sure what communicate is outputting in this case.
I was under the impression that it would output the text from my text file or it would output anything I type into the text file.
I tried typing into the newly opened text file as well, but I'm still getting the same output , b''
So what am I getting an empty string, despite typing something into the newly opened textfile.
Subprocess is basically as if you run that command in the terminal.
So what you are doing is running
notepad some_file.txt
which just opens a file in notepad, but it doesn't send anything to standard output.
If you run a command that writes something to standard output, then you will have a non-empty stdout1. Try ls for example if you are on a *nix system or dir if on Windows.

UnicodeEncodeError: 'charmap' codec can't encode... solution in traceback? [duplicate]

Ok, i want to print a string in my windows xp console.
There are several characters the console cant print, so i have to encode to my stdout.encoding which is 'cp437'. but printing the encoded string, the 'ß' is printed as '\xe1'. after decoding back to unicode and printing the string, i get the output i want. but this feels somewhat wrong. how is the correct way to print a string and get ? for non-printable characters?
>>>var
'Bla \u2013 großes'
>>>print(var)
UnicodeEncodeError: 'charmap' codec can't encode character '\u2013'
>>>var.encode('cp437', 'replace')
b'Bla ? gro\xe1es'
>>>print(var.encode('cp437', 'replace'))
b'Bla ? gro\xe1es'
>>>var.encode('cp437', 'replace').decode('cp437')
'Bla ? großes'
>>>print(var.encode('cp437', 'replace').decode('cp437'))
Bla ? großes
edit:
#Mark Ransom: since i print a lot this makes the code pretty bloated i feel :/
#eryksun: excactly what i was looking for. thanks a lot!
To print Unicode characters that can't be represented using the console codepage, you could use win-unicode-console Python package that uses Unicode API such as ReadConsoleW/WriteConsoleW() to read/write Unicode from/to Windows console directly:
#!/usr/bin/env python3
import win_unicode_console
win_unicode_console.enable()
try:
print('Bla \u2013 großes')
finally:
win_unicode_console.disable()
save it to test_unicode.py file, and run it:
C:\> py test_unicode.py
You should see:
Bla – großes
As a preferred alternative, you could use run module (included in the package), to run an ordinary script with enabled Unicode support in Windows console:
C:\> py -m run unmodified_script_that_prints_unicode.py
To install win_unicode_console module, run:
C:\> pip install win-unicode-console
Make sure to select a font able to display Unicode characters in Windows console.
To save the output of a Python script to a file, you could use PYTHONIOENCODING envvar:
C:\> set PYTHONIOENCODING=utf-8:backslashreplace
C:\> py unmodified_script_that_prints_unicode.py >output_utf8.txt
Do not hardcode the character encoding of your environment inside your script, print Unicode instead. The examples show that the same script may be used to print to the console and to a file using different encodings and different methods.
An alternate solution is to not use the crippled Windows console for general unicode output. Tk text widgets (accessed as tkinter Text instances) handle all BMP chars as long as the selected font will.
Since Idle used tkinter, it can as well. Running an Idle editor file (call it tem.py) containing
print('Bla \u2013 großes')
prints the following in the Shell window.
Bla – großes
A file can be run through Idle from the console with -m and -r.
C:\>python -m idlelib -r c:/programs/python34/tem.py
This opens a shell window and prints the same as above. Or you can create your own tk window with Label or Text widget.

Resources