python-3.4

Python 3 unicode to utf-8 on file

社会主义新天地 提交于 2019-12-12 21:19:12
问题 I am trying to parse through a log file, but the file format is always in unicode. My usual process that I would like to automate: I pull file up in notepad Save as... change encoding from unicode to UTF-8 Then run python program on it So this is the process I would like to automate in Python 3.4. Pretty much just changed the file to UTF-8 or something like open(filename,'r',encoding='utf-8') although this exact line was throwing me this error when I tried to call read() on it:

How to add album art to mp3 file using python 3?

二次信任 提交于 2019-12-12 12:15:23
问题 I was wondering what module to use for setting an image as the album art for a particular mp3 file. Mutagen seemed to be a popular choice, but it doesn't seem to work on python 3 and I can't find any documentation. 回答1: Here's a modified version of the code I use. You will want to change the example.mp3 and cover.jpg (and perhaps the mime type too): import eyed3 audiofile = eyed3.load('example.mp3') if (audiofile.tag == None): audiofile.initTag() audiofile.tag.images.set(3, open('cover.jpg',

Hide some maybe-no-member Pylint errors

纵饮孤独 提交于 2019-12-12 09:31:08
问题 The following Python fragment code gets analyzed by Pylint : if type(result) is array.array: read = result.tobytes() ... with the following error for the last line: E:401,22: Instance of 'int' has no 'tobytes' member\ (but some types could not be inferred) (maybe-no-member) The result variable is received from an external function. How can I change (correct) the code to make Pylint understand? Or how can I tell it that the result of the function can have other types than int? Or how can I

String performance - Python 2.7 vs Python 3.4 under Windows 10 vs. Ubuntu

女生的网名这么多〃 提交于 2019-12-12 08:05:44
问题 Use case A simple function which checks if a specific string is in another string at a position which is a multiple of 3 (see here for a real world example, finding stop codons in a DNA sequence). Functions sliding_window : takes a string of length 3 compares it to the search string, if they are identical moves 3 characters forward. incremental_start : tries to find the search string, if the found position is not a multiple of 3, it tries to find the next position after the found position.

python3: singledispatch in class, how to dispatch self type

本秂侑毒 提交于 2019-12-12 07:47:42
问题 Using python3.4. Here I want use singledispatch to dispatch different type in __mul__ method . The code like this : class Vector(object): ## some code not paste @functools.singledispatch def __mul__(self, other): raise NotImplementedError("can't mul these type") @__mul__.register(int) @__mul__.register(object) # Becasue can't use Vector , I have to use object def _(self, other): result = Vector(len(self)) # start with vector of zeros for j in range(len(self)): result[j] = self[j]*other return

How to loop on Python 3.4.2? [duplicate]

丶灬走出姿态 提交于 2019-12-12 06:16:58
问题 This question already has answers here : Asking the user for input until they give a valid response (18 answers) Closed 4 years ago . In my Python code, I have a part where I would like to repeatedly print a question if it doesn't match what I want it to. name1 = input("A male name (protagonist): ") if name1.endswith (('ly', 's')): print("Sorry mate, this doesn't seem to be a proper noun. Try it again.") name1 = input("A male name (protagonist): ") How do I make it repeatedly print out name1

Python: Can I open two Tkinter Windows at the same time?

只愿长相守 提交于 2019-12-12 04:08:18
问题 Is it possible to open 2 windows at the same time? import tkinter as Tk import random import math root = Tk.Tk() canvas = Tk.Canvas(root) background_image=Tk.PhotoImage(file="map.png") canvas.pack(fill=Tk.BOTH, expand=1) # Stretch canvas to root window size. image = canvas.create_image(0, 0, anchor=Tk.NW, image=background_image) root.wm_geometry("794x370") root.title('Map') root.mainloop() optimized_root = Tk.Tk() optimized_canvas = Tk.Canvas(optimized_root) optimized_root.pack(fill=Tk.BOTH,

how to check if the user input is a string in python 3

試著忘記壹切 提交于 2019-12-12 04:04:14
问题 I wrote the code below: try: nums=input("Write a name:") print (nums) except ValueError: print ("You didn't type a name") The problem is that even the user enter a number the program prints it ` 回答1: You can use the function yourstring.isalpha() it will return true if all characters in the string are from the alphabet. So for your example: nums = input("write a name:") if(not nums.isalpha()): print("you did not write a name!") return print(nums) 回答2: You can use the builtin Python function

How to use <<MenuSelect>> to bind to a tkinter menu item selection

帅比萌擦擦* 提交于 2019-12-12 02:25:55
问题 I've been trying to bind a callback to a menu item selection, instead of using the command= functionality of the add_command method. However, it seems that no matter what I try it will only give me a proper index of the menu item ("Menu 1" and "Menu 2") when they are select, instead of the index of the buttons of the menu. When the button is pressed in will just print None. This is my current test code, but I've been trying a bunch of different stuff. import tkinter as tk def menucallback

Python. Error using animation.FuncAnimation

懵懂的女人 提交于 2019-12-12 02:17:47
问题 I am new to python, and I want to display the graph in a new window after clicking the button, but I don't know what is the error occur as I followed the documentation. I keep getting the same error as below. Traceback (most recent call last): File "C:\Users\User\Desktop\IMPORTANT NOTES\python to firebase\GUI\increment.py", line 88, in <module> ani = animation.FuncAnimation(f, animate, interval=1000) File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib