I'm new to python and was playing around with how to change my Instagram profile picture. The part I just can't get past is how I can put my image into the program. This is my code:
from instagram_private_api import Client, ClientCompatPatch
user_name = 'my_username'
password = 'my_password'
api = Client(user_name, password)
api.change_profile_picture('image.png')
Now, from what I read on the API Documentation, I can't just put in an image. It needs to be byte data. On the API documentation, the parameter is described like this:
photo_data – byte string of image
I converted the image on an encoding website and now I have the file image.txt with the byte data of the image. So I changed the last line to this:
api.change_profile_picture('image.txt')
But this still doesn't work. The program doesn't read it as byte data. I get the following error:
Exception has occurred: TypeError
a bytes-like object is required, not 'str'
What is the right way to put in the picture?
The error is telling you that "input.txt" (or "image.png") is a string, and it's always going to say that as long as you pass in a filename because filenames are always strings. Doesn't matter what's in the file, because the API doesn't read the file.
It doesn't want the filename of the image, it wants the actual image data that's in that file. That's why the parameter is named photo_data and not photo_filename. So read it (in binary mode, so you get bytes rather than text) and pass that instead.
with open("image.png", "rb") as imgfile:
api.change_profile_picture(imgfile.read())
The with statement ensures that the file is closed after you're done with it.
if you have .png or .jpeg or ... then use this.
with open("image.png", "rb") as f:
api.change_profile_picture(f.read())
and if you have a .txt file then use this.
with open("image.txt", "rb") as f:
api.change_profile_picture(f.read())
I am trying to attach a PDF document to an email. I've come across this syntax:
add_attachment(*args, content_manager=None, *kw)
If the message is a multipart/mixed, create a new message object, pass all of the arguments to its set_content() method, and attach() it to the multipart. If the message is a non-multipart, multipart/related, or multipart/alternative, call make_mixed() and then proceed as above. If content_manager is not specified, use the content_manager specified by the current policy. If the added part has no Content-Disposition header, add one with the value attachment. This method can be used both for explicit attachments (Content-Disposition: attachment) and inline attachments (Content-Disposition: inline), by passing appropriate options to the content_manager.
I am not too sure how to use this to add a PDF document. Any ideas on how I can do this?
with open(r'C:\Users\David\Documents\Hello.pdf', 'rb') as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
This same syntax could also be used for .txt files. Possibly others too but I haven't tested it out beyond that.
I feel like this is a simple question but nonetheless I cannot find a straightforward answer.
I have an email (an .eml file) that I need to parse. This email has a data table in the body that I need to export to my database. I have been successful parsing data out of txt file emails and attached PDF files, so I understand concepts like mapping to where the data is stored as well as RegularExpressions, but these eml files I can't seem to figure out.
In my code below I have three blocks of code essentially trying to do the same thing (two of them are comments now). I am simply attempting to capture any, or all, of the data in the email. Each block of code produces the same error though:
TypeError: initial_value must be str or None, not _io.TextIOWrapper
I have read that this error is most likely due to Python expecting to receive a string but receives bytes instead, or vice versa. So I followed up those attempts by trying to implement io.StringIO or io.BytesIO but neither worked. I would like to be able to recognize and parse specific data out of the email.
Thank you for any help, as well as question asking criticism.
My code:
import email
#import io
import os
import re
path = 'Z:\\folderwithemlfile'
for filename in os.listdir(path):
file_path = os.path.join(path, filename)
if os.path.isfile(file_path):
with open(file_path, 'r', encoding="utf-8") as f:
b = email.message_from_string(f)
if b.is_multipart():
for paylod in b.get_payload():
print(payload.get_payload())
else:
print(b.get_payload())
#b = email.message_from_string(f)
#bbb = b['from']
#ccc = b['to']
#print(f)
#msg = email.message_from_string(f)
#msg['from']
#msg['to']
Picture of email:
i have a code that updates CSVs from a server. it gets data using:
a = urllib.urlopen(url)
data = a.read().strip()
then i append the data to the csv by
f = open(filename+".csv", "ab")
f.write(ndata)
f.close()
the problem is that randomly, a line in the csv gets written like this (or gets a line break somewhere along the csv):
2,,,,,
015-04-21 13:00:00,18,998,50,31,2293
instead of its usual form:
2015-04-21 13:00:00,6,1007,29,25,2394
2015-04-21 13:00:00,7,1004,47,26,2522
i tried printing my data in shell after the program ran, and it would show that the broken csv entry actually appears to be normal.
hope you guys can help me out. thanks.
running python 2.7.9 on win8.1
What actions are performed on your "ndata" variable ?
You should use the csv module to manage CSV files : https://docs.python.org/2/library/csv.html
Edit after comment :
If you do not want to use the "csv" module I linked to you, instead of
a = urllib.urlopen(url)
data = a.read().strip()
ndata = data.split('\n')
f.write('\n'.join(ndata[1:]))
you should do this :
a = urllib.urlopen(url)
f.writelines(a.readlines()[1:])
I don't see any reason explaining your randomly unwanted "\n" if you are sure that your incoming data is correct. Do you manage very long lines ?
I recommand you to use the csv module to read your input : you'll be sure to have a valid CSV content if your input is correct.
I'm newbie as with python as programming, poor English also...
I have a doubt, I'm using suds to get methods from a WSDL and then sometimes it returns me type instance or type text, when it returns me instance I could manipulate the object like a list, but like a text I couldn't, so I try to parse it, but it's too big, and the structure of the text there are a lot of "\n", so I thought, maybe I can read and treat like a file.txt and for each "\n" I get a list element. But I have no idea how I can turn a string or "Text" in .txt
Can you help me?
my python.py:
#!/usr/bin/python
from suds.client import Client
import xml.etree.ElementTree as ET
url = 'https://gpadev.servicedesk.net.br/dataservices/application/clients/clients.asmx?WSDL'
d = dict(http='******', https='********')
client = Client(url, proxy = d, username= '******', password = '********')
method = client.service.Export('*******')
type (method)
it returns me:
type text
if a print, I get something like:
CLIENT,FULLNAME,SEX,NICKNAME,BOSS,TITLE,MANAGER,INACTIVE,NETID,EMAILID,EMAILALT,NOTIFYMAIL,PAGERNUMBER,NOTIFYPAGER,PHONELBL1,PHONE1,PHONELBL2,PHONE2,PHONELBL3,PHONE3,ADDRESS,ADDRESS2,ZIP,CITY,STATE,DIVISION,REGION,LOCATION,ORGUNIT,CHARGE,SLEVEL,SKILL,LANGID,TIMEZONE,NOTES,CLIENT_LIST_MANAGELEVEL,ANALYST_LIST_PROFILE **\n** CLIENT,FULLNAME,SEX,NICKNAME,BOSS,TITLE,MANAGER,INACTIVE,NETID,EMAILID,EMAILALT,NOTIFYMAIL,PAGERNUMBER,NOTIFYPAGER,PHONELBL1,PHONE1,PHONELBL2,PHONE2,PHONELBL3,PHONE3,ADDRESS,ADDRESS2,ZIP,CITY,STATE,DIVISION,REGION,LOCATION,ORGUNIT,CHARGE,SLEVEL,SKILL,LANGID,TIMEZONE,NOTES,CLIENT_LIST_MANAGELEVEL,ANALYST_LIST_PROFILE **\n** .......**\n** .......**\n** .......**\n**
thanks for helping me
There are at least two things in your question:
how to split a string into a list of lines
how to save a string into an ASCII file (.txt)
For the first thing: it's as easy as calling lines=method.split('\n'), then you
can iterate through the returned lines list.
For the second thing:
with open("path to save the file + filename.txt", "w") as f:
f.write(method)