I'm new to python, I'm reading csv file having 2 columns as ID and Filepath (headers not present). Trying to enter the ID into the URL and filepath into the below rest api call. Can't get the values of the row. If the value at row[0] is TDEVOPS-1 it's returning numeric value.
import csv
filename1 = 'E:\\Upload-PM\\attachment.csv'
with open(filename1, 'rb') as csvfile:
datareader = csv.reader(csvfile)
for row in csvfile.readlines():
urlvalue = "https://<url>.atlassian.com/rest/api/3/issue/" str({row[0]}) "/attachments"
url = urlvalue
print(url)
headers = {"X-Atlassian-Token": "nocheck"}
files = {'file': open(row[1], 'rb')}
r = requests.post(url, auth=('<email>','<token>'), files=files, headers=headers)
print(r.status_code)
print(r.text)
Input:
TDEVOPST-5,E:\Upload-PM\att.csv
TDEVOPST-2,E:\Upload-PM\att2.csv
TDEVOPST-3,E:\Upload-PM\att3.csv
CodePudding user response:
It is not clear to me what is the exact error you are getting. But did you try using format?
urlvalue = "https://<url>.atlassian.com/rest/api/3/issue/{}/attachments".format(row[0])
UPDATE - corresponding to the comments, the issue seems to be with how you read the csv file. Id recommend to use the “r” flag for better string parsing. See - Difference between parsing a text file in r and rb mode for more details
In addition I'd suggest to use python os.path
to make sure the path is valid
CodePudding user response:
If your file has not header then try to read it as symply .txt file:
attachment = ["TDEVOPST-5,E:\\Upload-PM\\att.csv","TDEVOPST-2,E:\\Upload-PM\\att2.csv","TDEVOPST-3,E:\\Upload-PM\\att3.csv"]
fn = "temp.txt"
with open(fn, "w") as f:
f.write("\n".join(attachment))
with open(fn,"r") as f:
for row in f:
print(row.replace("\n","")) # optional string for test
els = row.split(",")
print(els[0],"->",els[1]) # optional string for test
Then you can use: els[0],els[1]
as you need. May be like this:
urlvalue = f"https://<url>.atlassian.com/rest/api/3/issue/{els[0]}/attachments"