How do I get this code to work in 3?
Please note that I am not asking about "foo".upper() at the string instance level.
import string
try:
print("string module, upper function:")
print(string.upper)
foo = string.upper("Foo")
print("foo:%s" % (foo))
except (Exception,) as e:
raise
output on 2:
string module, upper function:
<function upper at 0x10baad848>
foo:FOO
output on 3:
string module, upper function:
Traceback (most recent call last):
File "dummytst223.py", line 70, in <module>
test_string_upper()
File "dummytst223.py", line 63, in test_string_upper
print(string.upper)
AttributeError: module 'string' has no attribute 'upper'
help(string) wasn't very helpful either. Far as I can tell, the only function left is string.capwords.
Note: a bit hacky, but here's a my short-term workaround.
import string
try:
_ = string.upper
except (AttributeError,) as e:
def upper(s):
return s.upper()
string.upper = upper
All of the string module-level functions you describe were removed in Python 3. The Python 2 string module documentation contains this note:
You should consider these functions as deprecated, although they will not be removed until Python 3.
If you have string.upper(foo) in Python 2, you need to convert that to foo.upper() in Python 3.
Related
I'm using Jinja2 (3.0.1), Python3 (3.9.5), and macOS (11.3.1).
These lines used to work:
from jinja2 import Environment, PackageLoader
e = Environment(loader = PackageLoader("__main__", "."))
but now produce:
Traceback (most recent call last):
File "/Users/downing/Dropbox/jinja2/Jinja2.py", line 36, in <module>
main()
File "/Users/downing/Dropbox/jinja2/Jinja2.py", line 31, in main
e = Environment(loader = PackageLoader("__main__", "."))
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 286, in __init__
spec = importlib.util.find_spec(package_name)
File "/usr/local/Cellar/python#3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/util.py", line 114, in find_spec
raise ValueError('{}.__spec__ is None'.format(name))
ValueError: __main__.__spec__ is None
Just discovered that FileSystemLoader still works and looks more appropriate for my use anyway.
The PackageLoader docs say:
Changed in version 3.0: No longer uses setuptools as a dependency.
which seems significant.
__main__.__spec__ can be None if __main__ isn't imported from a module. Probably python -m Jinja2 would still work for you, even if python Jinja2.py doesn't.
Like you, I switched to FileSystemLoader for this case, but I added a conditional to continue using PackageLoader when possible. That allows python -m path.to.my.module to continue working if the module is installed in a ZIP file or egg:
if __spec__ is not None:
loader = jinja2.PackageLoader('__main__')
else:
loader = jinja2.FileSystemLoader(
os.path.join(os.path.dirname(__file__), 'templates')
)
env = jinja2.Environment(loader=loader)
I'd like to get the latest posts id from a subreddit. Reddit is have basic api for this. You can get json so i want gives data and decode it but i have a error.
root#archi-sunucu:~/yusuf/www# python3 reddit.py
Traceback (most recent call last):
File "reddit.py", line 24, in <module>
json = json.loads(resp.text())
TypeError: 'str' object is not callable
root#archi-sunucu:~/yusuf/www# python3 reddit.py
my code:
url = "https://www.reddit.com/r/" + subreddit + "/" + feed + ".json?sort=" + feed + "&limit=6"
resp = requests.get(url, verify=False)
json = json.loads(resp.text())
print(json["data"]["children"][0]["data"]["id"])
thanks for helps...
You complained that this expression raises an error:
json.loads(resp.text())
Well, let's break that down into something simpler,
so the line number tells us exactly what part of your code is failing.
temp = resp.text()
json.loads(temp)
Now we see that the 2nd line doesn't even execute,
it fails in the 1st line attempting to compute something
to assign to the temporary variable.
Examine resp and its attribute with tools
like help(resp), dir(resp), type(resp.text), repr(resp.text).
You will soon learn the .text attribute is a str.
That is not a callable function, so python raises an error.
Use the value directly, without a call:
json = json.loads(resp.text)
I'm using a NAO robot with naoqi version 2.1 and Choregraphe on Windows. I want to parse json from an attached file to the behavior. I attached the file like in that link.
Code:
def onLoad(self):
self.filepath = os.path.join(os.path.dirname(ALFrameManager.getBehaviorPath(self.behaviorId)), "fileName.json")
def onInput_onStart(self):
with open(self.filepath, "r") as f:
self.data = self.json.load(f.get_Response())
self.dataFromFile = self.data['value']
self.log("Data from file: " + str(self.dataFromFile))
But when I run this code on the robot (connected with a router) I'll get an error:
[ERROR] behavior.box :_safeCallOfUserMethod:281 _Behavior__lastUploadedChoregrapheBehaviorbehavior_1136151280__root__AbfrageKontostand_3__AuslesenJSONDatei_1: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/naoqi.py", line 271, in _safeCallOfUserMethod
func()
File "<string>", line 20, in onInput_onStart
File "/usr/lib/python2.7/site-packages/inaoqi.py", line 265, in <lambda>
__getattr__ = lambda self, name: _swig_getattr(self, behavior, name)
File "/usr/lib/python2.7/site-packages/inaoqi.py", line 55, in _swig_getattr
raise AttributeError(name)
AttributeError: json
I already tried to understand the code from the correspondending lines but I couldn't fixed the error. But I know that the type of my object f is 'file'. How can I open the json file as a json file?
Your problem comes from this:
self.json.load(f.get_Response())
... there is no such thing as "self.json" on a Choregraphe box, import json and then do json.load. And what is get_Response ? That method doesn't exist on anything in Python that I know of.
You might want to first try making a standalone python script (that doesn't use the robot) that can read your json file before you try it with choregraphe. It will be easier.
There's a bit of code giving me trouble. It was working great in another script I had but I must have messed it up somehow.
The if csv: is primarily because I was relying on a -csv option in an argparser. But even if I were to run this with proper indents outside the if statement, it still returns the same error.
import csv
if csv:
with open('output.csv', 'wb') as csvfile:
csvout = csv.writer(csvfile, delimiter=',',
quotechar=',', quoting=csv.QUOTE_MINIMAL)
csvout.writerow(['A', 'B', 'C'])
csvfile.close()
Gives me:
Traceback (most recent call last):
File "import csv.py", line 34, in <module>
csvout = csv.writer(csvfile, delimiter=',',
AttributeError: 'str' object has no attribute 'writer'
If I remove the if statement, I get:
Traceback (most recent call last):
File "C:\import csv.py", line 34, in <module>
csvout = csv.writer(csvfile, delimiter=',',
AttributeError: 'NoneType' object has no attribute 'writer'
What silly thing am I doing wrong? I did try changing the file name to things like test.py as I saw that in another SO post, didn't work.
For me I had named my file csv.py. So when I import csv from that file I was essentially trying to import the same file itself.
If you've set something that assigns to csv (looks like a string) then you're shadowing the module import. So, the simplest thing is to just change whatever's assigning to csv that isn't the module and call it something else...
In effect what's happening is:
import csv
csv = 'bob'
csvout = csv.writer(somefile)
Remove the further assignment to csv and go from there...
For my case, my function name happened to be csv(). Once I renamed my function, the error disappeared.
A simple program for reading a CSV file inside a ZIP archive:
import csv, sys, zipfile
zip_file = zipfile.ZipFile(sys.argv[1])
items_file = zip_file.open('items.csv', 'rU')
for row in csv.DictReader(items_file):
pass
works in Python 2.7:
$ python2.7 test_zip_file_py3k.py ~/data.zip
$
but not in Python 3.2:
$ python3.2 test_zip_file_py3k.py ~/data.zip
Traceback (most recent call last):
File "test_zip_file_py3k.py", line 8, in <module>
for row in csv.DictReader(items_file):
File "/somedir/python3.2/csv.py", line 109, in __next__
self.fieldnames
File "/somedir/python3.2/csv.py", line 96, in fieldnames
self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file
in text mode?)
The csv module in Python 3 wants to see a text file, but zipfile.ZipFile.open returns a zipfile.ZipExtFile that is always treated as binary data.
How does one make this work in Python 3?
I just noticed that Lennart's answer didn't work with Python 3.1, but it does work with Python 3.2. They've enhanced zipfile.ZipExtFile in Python 3.2 (see release notes). These changes appear to make zipfile.ZipExtFile work nicely with io.TextWrapper.
Incidentally, it works in Python 3.1, if you uncomment the hacky lines below to monkey-patch zipfile.ZipExtFile, not that I would recommend this sort of hackery. I include it only to illustrate the essence of what was done in Python 3.2 to make things work nicely.
$ cat test_zip_file_py3k.py
import csv, io, sys, zipfile
zip_file = zipfile.ZipFile(sys.argv[1])
items_file = zip_file.open('items.csv', 'rU')
# items_file.readable = lambda: True
# items_file.writable = lambda: False
# items_file.seekable = lambda: False
# items_file.read1 = items_file.read
items_file = io.TextIOWrapper(items_file)
for idx, row in enumerate(csv.DictReader(items_file)):
print('Processing row {0} -- row = {1}'.format(idx, row))
If I had to support py3k < 3.2, then I would go with the solution in my other answer.
Update for 3.6+
Starting w/3.6, support for mode='U' was removed^1:
Changed in version 3.6: Removed support of mode='U'. Use io.TextIOWrapper for reading compressed text files in universal newlines mode.
Starting w/3.8, a Path object was added which gives us an open() method that we can call like the built-in open() function (passing newline='' in the case of our CSV) and we get back an io.TextIOWrapper object the csv readers accept. See Yuri's answer, here.
You can wrap it in a io.TextIOWrapper.
items_file = io.TextIOWrapper(items_file, encoding='your-encoding', newline='')
Should work.
And if you just like to read a file into a string:
with ZipFile('spam.zip') as myzip:
with myzip.open('eggs.txt') as myfile:
eggs = myfile.read().decode('UTF-8'))
Lennart's answer is on the right track (Thanks, Lennart, I voted up your answer) and it almost works:
$ cat test_zip_file_py3k.py
import csv, io, sys, zipfile
zip_file = zipfile.ZipFile(sys.argv[1])
items_file = zip_file.open('items.csv', 'rU')
items_file = io.TextIOWrapper(items_file, encoding='iso-8859-1', newline='')
for idx, row in enumerate(csv.DictReader(items_file)):
print('Processing row {0}'.format(idx))
$ python3.1 test_zip_file_py3k.py ~/data.zip
Traceback (most recent call last):
File "test_zip_file_py3k.py", line 7, in <module>
items_file = io.TextIOWrapper(items_file,
encoding='iso-8859-1',
newline='')
AttributeError: readable
The problem appears to be that io.TextWrapper's first required parameter is a buffer; not a file object.
This appears to work:
items_file = io.TextIOWrapper(io.BytesIO(items_file.read()))
This seems a little complex and also it seems annoying to have to read in a whole (perhaps huge) zip file into memory. Any better way?
Here it is in action:
$ cat test_zip_file_py3k.py
import csv, io, sys, zipfile
zip_file = zipfile.ZipFile(sys.argv[1])
items_file = zip_file.open('items.csv', 'rU')
items_file = io.TextIOWrapper(io.BytesIO(items_file.read()))
for idx, row in enumerate(csv.DictReader(items_file)):
print('Processing row {0}'.format(idx))
$ python3.1 test_zip_file_py3k.py ~/data.zip
Processing row 0
Processing row 1
Processing row 2
...
Processing row 250
Starting with Python 3.8, the zipfile module has the Path object, which we can use with its open() method to get an io.TextIOWrapper object, which can be passed to the csv readers:
import csv, sys, zipfile
# Give a string path to the ZIP archive, and
# the archived file to read from
items_zipf = zipfile.Path(sys.argv[1], at='items.csv')
# Then use the open method, like you'd usually
# use the built-in open()
items_f = items_zipf.open(newline='')
# Pass the TextIO-like file to your reader as normal
for row in csv.DictReader(items_f):
print(row)
Here's a minimal recipe to open a zip file and read a text file inside that zip. I found the trick to be the TextIOWrapper read() method, not mentioned in any answers above (BytesIO.read() was mentioned above, but Python docs recommend TextIOWrapper).
import zipfile
import io
# Create the ZipFile object
zf = zipfile.ZipFile('my_zip_file.zip')
# Read a file that is inside the zip...reads it as a binary file-like object
my_file_binary = zf.open('my_text_file_inside_zip.txt')
# Convert the binary file-like object directly to text using TextIOWrapper and it's read() method
my_file_text = io.TextIOWrapper(my_file_binary, encoding='utf-8', newline='').read()
I wish they kept the mode='U' parameter in the ZipFile open() method to do this same thing since that was so succinct but, alas, that is obsolete.