Problem /bin/sh: 0: Illegal option -* running code in python3 - python-3.x
Error image and editor configured in UTF8
I am trying to run the following code in python 3.7.5
pi = 3.14159
radius = 15.3
print ('Circle Area is', pi * radius ** 2)
does not run error appears: SyntaxError: Non-ASCII character '\ xc3' in file
adding the command line #! - * - conding: utf8 - * - the following error appears:
/ bin / sh: 0: Illegal option - *
It should be without spaces and also its not conding its coding
# -*- coding: utf-8 -*-
not
#! - * - conding: utf8 - * -
You don't need this line with Python 3. The default encoding is UTF-8 with Python 3+.
Related
SyntaxError when using print(""" with a lst of numbers to populate a file with GROMACS patched with PLUMED
I am using GROMACS with PLUMED to run MD simulations. In setting up my plumed file for collecting the S2/SH CV from Omar(https://www.plumed.org/doc-v2.8/user-doc/html/_s2_c_m.html) I am having difficulties with the line: File "makingplumed.py", line 25 """ % (x,i)file=f) ^ SyntaxError: invalid syntax Here is the code I am trying to run: # here we create the PLUMED input file with python with open("plumed.dat","w") as f: # print initial stuff #K# from __future__ import print_function # Define Atoms which are Oxygen hydrogen bond acceptors ATOMS=[21,35,45,62,76,97,109,133,152,174,188,202,213,227,239,253,269,280,292,311,323,339,353,377,401,416,426,447,466,477,488,503,518,538,560,575,597,617,624,641,655,677,692,702,722,743,765,784,798,820,844,866,883,897,919,939,961,978,988,1004,1021,1040] #Define heavy atoms for S2CM CV (protein and backbone and not hydrogen) heavy_atoms_nh: GROUP ATOMS=1,5,7,10,12,16,20,21,22,23,26,29,32,34,35,36,38,40,44,45,46,48,50,53,54,55,57,59,61,62,63,64,67,70,73,75,76,79,81,84,85,87,89,90,92,94,96,97,98,100,102,105,106,107,108,109,110,112,114,117,120,123,124,125,126,129,132,133,134,136,138,141,143,147,151,152,153,155,157,160,163,166,169,173,174,175,177,179,181,185,187,188,189,191,193,195,199,201,202,203,205,207,210,212,213,214,216,217,218,220,224,226,227,228,230,232,235,236,237,238,239,240,241,244,247,250,252,253,254,256,258,260,264,268,269,270,272,274,277,279,280,281,283,285,288,291,392,293,295,297,299,303,306,310,311,312,314,316,319,320,321,322,323,326,328,330,334,338,339,340,342,344,346,350,352,353,354,356,358,361,364,367,369,370,373,376,377,378,380,382,385,388,391,393,397,400,401,402,404,406,409,412,415,416,417,419,421,425,426,427,429,431,434,345,437,439,442,444,446,447,448,450,452,455,457,461,465,466,467,469,471,474,476,477,478,480,482,485,487,489,491,493,496,499,500,501,502,503,504,506,508,511,514,515,516,517,518,519,521,523,526,527,529,531,533,535,537,538,539,541,543,546,549,552,555,559,560,561,563,265,268,571,572,573,574,575,576,578,580,583,586,589,592,596,597,598,600,602,605,606,608,610,612,614,616,617,618,620,623,624,625,627,629,632,635,636,640,641,642,644,646,648,652,654,655,656,658,660,663,666,669,672,676,677,678,680,682,685,688,691,692,693,695,697,701,702,703,705,707,710,711,713,715,717,719,721,722,723,725,727,730,731,733,735,738,740,742,743,744,746,748,751,754,757,760,764,765,766,768,770,773,775,779,783,784,785,786,789,792,795,797,798,799,801,803,806,809,812,815,819,820,821,823,825,828,829,831,833,834,836,838,840,842,843,844,845,847,849,852,855,858,861,865,866,867,869,871,874,877,878,879,882,883,884,886,888,891,892,893,896,867,898,900,902,905,908,911,914,918,919,920,922,924,927,928,930,932,934,936,938,939,940,942,944,947,950,953,956,960,961,962,964,966,969,975,973,977,978,979,981,983,987,988,989,991,993,995,999,1003,1004,1005,1007,1009,1012,1015,1016,1017,1020,1021,1022,1024,1026,1029,1031,1035,1039,1040,1041,1043,1045,1048,1049,1051,1053,1055,1057,1059,1060,1061 for x in range(len(ATOMS)): for i in range(1, 60): print(""" S2CM ... NH_ATOMS=x,x+2 HEAVY_ATOMS=heavy_atoms_nh LABEL=S2nh-%d R_EFF=0.10 PREFACTOR_A=0.80 EXPONENT_B=1.0 OFFSET_C=0.10 N_I=1 NOPBC ... S2CM """ % (x,i)file=f) I am just learning python and Linux this summer as I am getting involved with computational biochemistry research, so if there is a simple fix I am very sorry for the waste of time, and I appreciate any and all time and attention to this matter. python3 --version Python 3.6.13 Thank You, David Cummins Masters Student at Western Washington University
Mimicking bash wc functionalities using python
I have written a very simple python programme, called wc.py, which mimics "bash wc" behaviour to count the number of words, lines and bytes in a file. My programme is as follow: import sys path = sys.argv[1] w = 0 l = 0 b = 0 for currentLine in file: wordsInLine = currentLine.strip().split(' ') wordsInLine = [word for word in wordsInLine if word != ''] w += len(wordsInLine) b += len(currentLine.encode('utf-8')) l += 1 #output print(str(l) + ' ' + str(w) + ' ' + str(b)) In order to execute my programme you should execute the following command: python3 wc.py [a file to read the data from] As the result it shows [The number of lines in the file] [The number of words in the file] [The number of bytes in the file] [the file directory path] The files I used to test my code is as follow: file.txt which contains the following data: 1 2 3 4 Executing "wc file.txt" returns 4 4 8 Executing "python3 wc.py file.txt" returns 4 4 8 Download "Annual enterprise survey: 2020 financial year (provisional) – CSV" from CSV file download Executing "wc [fileName].csv" returns 37081 500273 5881081 Executing "python3 wc.py [fileName].csv" returns 37081 500273 5844000 and a [something].pdf file Executing "wc [something].pdf" works. Executing "python3 code.py" throws the following errors: Traceback (most recent call last): File "code.py", line 10, in <module> for currentLine in file: File "/usr/lib/python3.8/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 10: invalid start byte As you can see, the output of python3 code.py [something].pdf and python3 code.py [something].csv is not the same as what wc returns. Could you help me to find the reason of this erroneous behaviour in my code?
Regarding the CSV file, if you look at the difference between your result and that of wc: 5881081 - 5844000 = 37081 which is exactly the number of lines. That is, every line has one additional character in the original file. That character is the carriage return \r which got lost in Python because you iterate over lines and don't specify the linebreaks. If you want a byte-correct result, you have to first identify the type of linebreaks used in the file (and watch out for inconsistencies throughout the document).
How to write Chinese characters to file based on unicode code point in Python3
I am trying to write Chinese characters to a CSV file based on their Unicode code points found in a text file in unicode.org/Public/zipped/13.0.0/Unihan.zip. For instance, one example character is U+9109. In the example below I can get the correct output by hard coding the value (line 8), but keep getting it wrong with every permutation I've tried at generating the bytes from the code point (lines 14-16). I'm running this in Python 3.8.3 on a Debian-based Linux distro. Minimal working (broken) example: 1 #!/usr/bin/env python3 2 3 def main(): 4 5 output = open("test.csv", "wb") 6 7 # Hardcoded values work just fine 8 output.write('\u9109'.encode("utf-8")) 9 10 # Comma separation 11 output.write(','.encode("utf-8")) 12 13 # Problem is here 14 codepoint = '9109' 15 u_str = '\\' + 'u' + codepoint 16 output.write(u_str.encode("utf-8")) 17 18 # End with newline 19 output.write('\n'.encode("utf-8")) 20 21 output.close() 22 23 if __name__ == "__main__": 24 main() Executing and viewing results: example $ example $./test.py example $ example $cat test.csv 鄉,\u9109 example $ The expected output would look like this (Chinese character occurring on both sides of the comma): example $ example $./test.py example $cat test.csv 鄉,鄉 example $
chr is used to convert integers to code points in Python 3. Your code could use: output.write(chr(0x9109).encode("utf-8")) But if you specify the encoding in the open instead of using binary mode you don't have to manually encode everything. print to a file handles newlines for you as well. with open("test.txt",'w',encoding='utf-8') as output: for i in range(0x4e00,0x4e10): print(f'U+{i:04X} {chr(i)}',file=output) Output: U+4E00 一 U+4E01 丁 U+4E02 丂 U+4E03 七 U+4E04 丄 U+4E05 丅 U+4E06 丆 U+4E07 万 U+4E08 丈 U+4E09 三 U+4E0A 上 U+4E0B 下 U+4E0C 丌 U+4E0D 不 U+4E0E 与 U+4E0F 丏
python : error encoding 'ascii' ONLY running with crontab
My python script working very well when i'm running it in console mode. It's write a html file, convert with pdfkit in pdf and send a mail. All is allright. But, when this script running from the crontab, there's some encoding error. if len(html) > 0: with open(file_html_to_pdf, 'w') as file: message = '[PDF] >> writing the html file "{0}"'.format(file_html_to_pdf) logging.info(message) print(message) file.write(html) else: raise IOError('html file is invalid !') In the crontab, I've done this : # VARIABLES FOR PYTHON PYTHONIOENCODING=utf8 */5 * * * * /home/users/my-user/my-project/env/bin/python /home/users/my-user/my-project/cronjob.py > /var/log/apps/my-project/cron_error_my-project.log 2>&1 And in the bashrc : # set locale utf-8 in french.....et voilà export PYTHONIOENCODING=utf-8 export LC_ALL=fr_FR.utf-8 export LANG="$LC_ALL" The error message : UnicodeEncodeError: 'ascii' codec can't encode character '\xf4' in position 37: ordinal not in range(128) ... UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 400: ordinal not in range(128) ... Message: ('<!> Error => ', UnicodeEncodeError('ascii', '<!DOCTYPE html>\n<head>\n <title>--- Python version : $ /home/users/my-user/my-project/env/bin/python --version Python 3.4.2 I don't understand why :(. Can anybody help me? Thanks F.
Why does unicode to string only work with try/except?
Just when I thought I had my head wrapped around converting unicode to strings Python 2.7 throws an exception. The code below loops over a number of accented characters and converts them to their non-accented equivalents. I've put in an special case for the double s. #!/usr/bin/python # -*- coding: utf-8 -*- import unicodedata def unicodeToString(uni): return unicodedata.normalize("NFD", uni).encode("ascii", "ignore") accentList = [ #(grave accent) u"à", u"è", u"ì", u"ò", u"ù", u"À", u"È", u"Ì", u"Ò", u"Ù", #(acute accent) u"á", u"é", u"í", u"ó", u"ú", u"ý", u"Á", u"É", u"Í", u"Ó", u"Ú", u"Ý", #(arrete accent) u"â", u"ê", u"î", u"ô", u"û", u"Â", u"Ê", u"Î", u"Ô", u"Û", #(tilde ) u"ã", u"ñ", u"õ", u"Ã", u"Ñ", u"Õ", #(diaresses) u"ä", u"ë", u"ï", u"ö", u"ü", u"ÿ", u"Ä", u"Ë", u"Ï", u"Ö", u"Ü", u"Ÿ", #ring u"å", u"Å", #ae ligature u"æ", u"Æ", #oe ligature u"œ", u"Œ", #c cidilla u"ç", u"Ç", # D stroke? u"ð", u"Ð", # o slash u"ø", u"Ø", u"¿", # Spanish ? u"¡", # Spanish ! u"ß" # Double s ] for i in range(0, len(accentList)): try: u = accentList[i] s = unicodeToString(u) if u == u"ß": s = "ss" print("%s -> %s" % (u, s)) except: pass Without the try/except I get an error: File "C:\Python27\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\xc0' in position 0 : character maps to <undefined> Is there anything I can do to make the code run without using the try/except? I'm using Sublime Text 2.
try/except does not make Unicode work. It just hides errors. To fix the UnicodeEncodeError error, drop try/except and see Python, Unicode, and the Windows console.