how to write my terminal in a text file using python - python

Partial of my code is below. I want to export output of terminal in a text file but I get below error:
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-2-c7d647fa741c> in <module>()
34 text_file = open("Output.txt", "w")
35
---> 36 text_file.write(data)
37 #print (data)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 150-151: ordinal not in range(128)
# data is multi line text
data = ''.join(soup1.findAll('p', text=True))
text_file = open("Output.txt", "w")
text_file.write(data)
# print (data)

Encode your text before you write to the file:
text_file.write(data.encode("utf-8"))

Related

Program crashes during reading text file

def process_file(self):
error_flag = 0
line_count = 0
log_file = self.file_name
pure_name = log_file.strip()
# print('Before opening file ',pure_name)
logfile_in = open(pure_name, 'r') # Read file
lines = logfile_in.readlines()
# print('After reading file enteries ', pure_name)
Error Message
Traceback (most recent call last):
File "C:\Users\admin\PycharmProjects\BackupLogCheck\main.py", line 49, in <module>
backupLogs.process_file()
File "C:\Users\admin\PycharmProjects\BackupLogCheck\main.py", line 20, in process_file
lines = logfile_in.readlines()
File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 350: character maps to <undefined>
Process finished with exit code 1
Line 49 is where I call above method. But I have traced that it crashes at reading the file. I have checked the file; it has just text in it. I don't know if there are some characters which it doesn't like on reading entries. I am running on Windows 10.
I am new to Python, any suggestion how to find/correct the issue?
Try the file name in string format
logfile_in = open('pure_name', 'r') # Read file
lines = logfile_in.readlines()
print(lines)
output
['test line one\n', 'test line two']
or
logfile_in = open('pure_name', 'r') # Read file
lines = logfile_in.readlines()
for line in lines:
print(line)
output
test line one
test line two

having problems in module

i am making a discord bot heres the code and the error
f = open("rules.txt","r")
rules = f.readlines()
error:
Traceback (most recent call last):
File "C:\Users\Windows10\OneDrive\Desktop\YourBot\bot.py", line 8, in <module>
rules = f.readlines()
File "C:\Users\Windows10\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 7: character maps to <undefined>
please help me..
Kindly Try
First:
f = open('rules.txt', 'r', encoding='utf8')
rules = f.readlines()
Second:
f = open('rules.txt', 'r', errors = 'ignore')
rules = f.readlines()

UnicodeDecodeError: Utf-8 Decode Error while parsing Midi File

I was trying to parse a few music files using music21, but the code is generating utf-8 error:
'utf-8' codec can't decode byte 0xe9 in position 3: invalid continuation byte
Here is my code:
notes = []
for file in glob.glob("midi_songs/*.mid"):
print("parsing %s"%file)
midi = converter.parse(file)
elements_to_parse = midi.flat.notes
for ele in elements_to_parse:
#Note: Store Pitch
if isinstance(ele, note.Note):
notes.append(str(ele.pitch))
#Chorde: Split note and join
elif isinstance(ele, chord.Chord):
notes.append("+".join(str(n) for n in ele.normalOrder))
Traceback
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-17-ca5986c7b6d6> in <module>
2 for file in glob.glob("midi_songs/*.mid"):
3 print("parsing %s"%file)
----> 4 midi = converter.parse(file)
5
6 elements_to_parse = midi.flat.notes
This is a regression in music21 6.1.0 that was fixed in 6.3.0
Each problematic file we found to have a copyright symbol in the track name message and was written by www.piano-midi.de.

Converting .arff file to .csv using Python

I have a file "LMD.rh.arff" which I am trying to convert to .csv file using the following code-
import pandas as pd
import matplotlib.pyplot as plt
from scipy.io import arff
# Read in .arff file-
data = arff.loadarff("LMD.rh.arff")
But this last line of code gives me the error-
--------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call
last) in
----> 1 data = arff.loadarff("LMD.rp.arff")
~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in
loadarff(f)
539 ofile = open(f, 'rt')
540 try:
--> 541 return _loadarff(ofile)
542 finally:
543 if ofile is not f: # only close what we opened
~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in
_loadarff(ofile)
627 a = generator(ofile)
628 # No error should happen here: it is a bug otherwise
--> 629 data = np.fromiter(a, descr)
630 return data, meta
631
UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in
position 4: ordinal not in range(128)
In [6]: data = arff.loadarff("LMD.rh.arff")
--------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call
last) in
----> 1 data = arff.loadarff("LMD.rh.arff")
~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in
loadarff(f)
539 ofile = open(f, 'rt')
540 try:
--> 541 return _loadarff(ofile)
542 finally:
543 if ofile is not f: # only close what we opened
~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in
_loadarff(ofile)
627 a = generator(ofile)
628 # No error should happen here: it is a bug otherwise
--> 629 data = np.fromiter(a, descr)
630 return data, meta
631
UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in
position 4: ordinal not in range(128)
You can download the file arff_file
Any ideas as to what's going wrong?
Thanks!
Try this
path_to_directory="./"
files = [arff for arff in os.listdir(path_to_directory) if arff.endswith(".arff")]
def toCsv(content):
data = False
header = ""
newContent = []
for line in content:
if not data:
if "#attribute" in line:
attri = line.split()
columnName = attri[attri.index("#attribute")+1]
header = header + columnName + ","
elif "#data" in line:
data = True
header = header[:-1]
header += '\n'
newContent.append(header)
else:
newContent.append(line)
return newContent
# Main loop for reading and writing files
for zzzz,file in enumerate(files):
with open(path_to_directory+file , "r") as inFile:
content = inFile.readlines()
name,ext = os.path.splitext(inFile.name)
new = toCsv(content)
with open(name+".csv", "w") as outFile:
outFile.writelines(new)
Take a look at the error trace
UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in position 4: ordinal not in range(128)
Your error suggests you have some encoding problem with the file. Consider first opening the file with the correct encoding and then loading it to the arff loader
import codecs
import arff
file_ = codecs.load('LMD.rh.arff', 'rb', 'utf-8') # or whatever encoding you have
arff.load(file_) # now this should be fine
For reference see here

python - How to write unicode characters to files correctly

If I run the following code:
text = 'سلام عزیزم! عزیزم سلام!'
with open('temp.txt', 'w') as out_file:
print(text)
out_file.write(text)
with open('temp.txt', 'r') as in_file:
print(in_file.read())
I get this output:
Traceback (most recent call last):
سلام عزیزم! عزیزم سلام!
File "Z:/my files/projects/programing/python/courseAssist/gui.py", line 190, in <module>
out_file.write(text)
File "C:\Users\aran\Anaconda3\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 0-3: character maps to <undefined>
How can I fix it?
Specify the encoding encoding='utf-8':
text = 'سلام عزیزم! عزیزم سلام!'
with open('temp.txt', 'w', encoding='utf-8') as out_file:
print(text)
out_file.write(text)
with open('temp.txt', 'r', encoding='utf-8') as in_file:
print(in_file.read())

Categories