python unicode replace MemoryError - python

i want replace unicode character to a file with python
this is my code :
with codecs.open('/etc/bluetooth/main.conf', "r", "utf8") as fi:
mainconf=fi.read()
forrep = ''.decode('utf8')
for line in mainconf.splitlines():
if('Name = ' in line):
forrep = line.split('=')[1]
print 'name',type(name)
print 'mainconf',type(mainconf)
print 'forrep',type(forrep)
mainconf = mainconf.replace(forrep, name)
#mainconf = mainconf.replace(forrep.decode('utf8'),' '+name)
with codecs.open('/etc/bluetooth/main.conf','w',"utf8") as fi:
fi.write(mainconf)
but python always get me error MemoryError...
this out :
name <type 'unicode'>
mainconf <type 'unicode'>
forrep <type 'unicode'>
Traceback (most recent call last):
File "WORK/Bluetooth/Bluetooth.py", line 359, in <module>
if __name__ == '__main__':main()
File "WORK/Bluetooth/Bluetooth.py", line 336, in main
BLMan.SetAllHCIName(common.cfg.get('BLUETOOTH', 'HCI_DEVICE_NAME'))
File "WORK/Bluetooth/Bluetooth.py", line 194, in SetAllHCIName
mainconf = mainconf.replace(forrep, name)
MemoryError

Iterate over the file object, you are storing the whole file content in memory using mainconf=fi.read() :
with codecs.open('/etc/bluetooth/main.conf', "r", "utf8") as fi:
for line in fi:
You store all the lines with read then you store a list of all the lines using splitlines so you are storing all the file content twice and as #abarnet pointed out in a comment you then try to store a third copy with
mainconf = mainconf.replace(forrep, name).
Iterating over the file object will give you a line at a time, if you need to store the lines after replacing do so each time through the loop so at most you will only have one copy of the file content in memory.
I have no idea what name is but writing to a tempfile will be the most efficient way to do what you want:
from tempfile import NamedTemporaryFile
with open('/etc/bluetooth/main.conf') as fi, NamedTemporaryFile(dir=".", delete=False) as out:
for line in fi:
if line.startswith("Name ="):
a, b = line.split("=",1)
out.write("{} = {}".format(a, name.encode("utf-8")))
else:
out.write(line)
move(out.name, '/etc/bluetooth/main.conf')

Related

Replace Carriage Return (CR) and Carriage Return and Line Feed (CRLF) python

I import a .csv file that looks like:
by using the following code:
filetoread = 'G:/input.csv'
filetowrite = 'G:/output.csv'
# https://stackoverflow.com/questions/17658055/how-can-i-remove-carriage-return-from-a-text-file-with-python/42442131
with open(filetoread, "rb") as inf:
with open(filetowrite, "wb") as fixed:
for line in inf:
# line = line.replace('\r\r\n', 'r\n')
fixed.write(line)
print(line)
Which give the output:
b'\xef\xbb\xbfHeader1;Header2;Header3;Header4;Header5;Header6;Header7;Header8;Header9;Header10;Header11;Header12\r\n'
b';;;1999-01-01;;;01;;;;;;\r\n'
b';;;2000-01-01;;;12;;"NY123456789\r\r\n'
b'";chapter;2020-01-01 00:00:00;PE\r\n'
b';;;2020-01-01;;;45;;"NY123456789\r\r\n'
b'";chapter;1900-01-01 00:00:00;PE\r\n'
b';;;1999-01-01;;;98;;;;;;\r\n'
I have issues to replace \r\r\n to \r\n which I guess I need to do to get my desired output.
The error I get when I try to replace the \r\r\n is:
Traceback (most recent call last):
File "g:/till_format_2.py", line 10, in <module>
line = line.replace('\r\r\n', 'r\n')
TypeError: a bytes-like object is required, not 'str'
My desired output:
What do I need to add or change to the code to achieve my desired output?
As the error message says, supply a bytes object.
line = line.replace(b'\r\r\n', b'\r\n')
To get the desired output
line = line.replace(b'\r\r\n', b'')

Why do I receive an error stating coercing to unicode in Python?

I am trying to write a code that will fetch an api from my csv as input:
from pyzillow.pyzillow import ZillowWrapper, GetDeepSearchResults, GetUpdatedPropertyDetails
def get_zillowinfo(address,zipcode):
zillow_data = ZillowWrapper('X1-ZWz17seirkzuh7_93aho')
deep_search_response = zillow_data.get_deep_search_results(address,zipcode)
result1 = GetDeepSearchResults(deep_search_response) #get zillowid from address
updated_property_details_response = zillow_data.get_updated_property_details(result1.zillow_id)
result2 = GetUpdatedPropertyDetails(updated_property_details_response) # get detail property info
result = result2.home_info
return result
print get_zillowinfo('73 main blvd','01545')
#2
import csv
with open(r'C:\Users\bca\Desktop\Pricing Study-Zillow\sample4.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile,delimiter=',')
next(spamreader)
for row in spamreader:
print row
#3
import csv
with open(r'C:\Users\bca\Desktop\Pricing Study-Zillow\sample4.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile,delimiter=',')
next(spamreader)
for row in spamreader:
print get_zillowinfo(row[0],row[1])
When i do step #3, I get an error:
Traceback (most recent call last):
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2895, in run_code
self.showtraceback()
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 1828, in showtraceback
self._showtraceback(etype, value, stb)
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\ipykernel\zmqshell.py", line 547, in _showtraceback
u'evalue' : py3compat.safe_unicode(evalue),
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\ipython_genutils\py3compat.py", line 65, in safe_unicode
return unicode_type(e)
TypeError: coercing to Unicode: need string or buffer, dict found
Why does this happen? is it because my characters are not strings? how do I change it to string in that case for all my data?
This is a repex of my dataset:
What do I need to change in my code to avoid that type error?

json file load issue using Python

I was try to dump and load dictionary to json file using Python. I can dump file without a problem. However, when i try to load file into temp dictionary, error occur. I can not figure out the issue, Can anyone can help me on this ?Thanks
import os
import json
def get_stored_birth():
filename ='C:/Users/Sam/name.json'
temp = {}
with open(filename,'r+') as f_obj1:
temp =json.load(f_obj1)
print(temp.get(name),"is the birthday of",name)
def get_new_birth():
birth=str(input())
my_dict[name]=birth
print("Birthday database updated")
filename ='C:/Users/Sam/name.json'
with open(filename,'a') as f_obj:
f_obj.write('\n')
json.dump(my_dict,f_obj)
return name
my_dict={}
def quit():
"""This function quit program"""
return quit
while True:
filename ='C:/Users/Sam/name.json'
print("Enter a name:(blank to quit)")
name= str(input())
if name=="":
exit()
if name in my_dict:
name= get_stored_birth()
else:
print("I dont have info for",name)
print("What is their birthday")
name= get_new_birth()
The traceback as follow:
Traceback (most recent call last):
File "C:\Users\Sam\Desktop\Tianxu_Assignment2\Assignment 2.py", line 45, in <module>
name= get_stored_birth()
File "C:\Users\Sam\Desktop\Tianxu_Assignment2\Assignment 2.py", line 10, in get_stored_birth
temp =json.load(f_obj1)
File "C:\Users\Sam\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Sam\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Sam\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 3 column 1 (char 12)
Problem solved !!!
1. replace with open(filename, 'a') as f_obj with replace with open(filename, 'w')
2.
if name in my_dict:
should not check my_dict !!! every time start a program will use new "dictionary". I move
filename ='C:/Users/Sam/name.json'
temp = {}
with open(filename,'r+') as f_obj1:
temp =json.load(f_obj1)
to main loop and check if name in temp:
Thanks guys!!!
You are appending new json to previous jsons you have created. Just substitute this line:
with open(filename,'a') as f_obj:
with this one:
with open(filename,'w') as f_obj:

Python - TypeError: expected string or bytes-like object

I have the following code:
import re
meshTerm = {}
meshNumber = {}
File = 'file.bin'
with open(File, mode='rb') as file:
readFile = file.read()
outputFile = open('output.txt', 'w')
for line in readFile:
term= re.search(r'MH = .+', line)
print(term)
When I run the code, I get the following error:
Traceback (most recent call last):
File "myFile.py", line 13, in <module>
term = re.search(r'MH = .+', line)
File "C:\Python35\lib\re.py", line 173, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object
Why is that? How can I solve the issue?
Thanks.
You are reading the whole file using binary mode 'rb' in this line;
with open(File, mode='rb') as file:
readFile = file.read()
So that makes your readFile a bytes array, and when you loop through the readFile in the following manner it gives you a single byte. Which python assumes is an integer.
>> for line in readFile:
>> print(line)
>> print(type(line))
116
<class 'int'>
104
<class 'int'>
105
<class 'int'>
...
I think you meant to read the file line by line;
with open(File, mode='rb') as file:
readFile = file.readlines()

Why do I receive this error on parsing?

I am reading in a textfile and converting it into a python dictionary:
The file looks like this with labelword:
20001 World Economies
20002 Politics
20004 Internet Law
20005 Philipines Elections
20006 Israel Politics
20007 Science
This is the code to read the file and create a dictionary:
def get_pair(line):
key, sep, value = line.strip().partition("\t")
return int(key), value
with open("mapped.txt") as fd:
d = dict(get_pair(line) for line in fd)
print(d)
I receive {} when I print the contents of d.
Additionally, I receive this error:
Traceback (most recent call last):
File "predicter.py", line 23, in <module>
d = dict(get_pair(line) for line in fd)
File "predicter.py", line 23, in <genexpr>
d = dict(get_pair(line) for line in fd)
File "predicter.py", line 19, in get_pair
return int(key), value
ValueError: invalid literal for int() with base 10: ''
What does this mean? I do have content inside the file, I am not sure why is it not being read.
It means key is empty, which in turn means you have a line with a \t tab at the start or an empty line:
>>> '\tScience'.partition('\t')
>>> ''.partition('\t')
('', '', '')
My guess is that it is the latter; you can skip either such lines in your generator expression:
d = dict(get_pair(line) for line in fd if '\t' in line.strip())
Because line.strip() returns the lines without leading and trailing whitespace, empty lines or lines with only a tab at the start result in a string without a tab in it altogether. This won't handle all cases, but you could also strip the value passed to get_pair():
d = dict(get_pair(line.strip()) for line in fd if '\t' in line.strip())

Categories

Resources