I am new to Python and I am trying to export my output to CSV with headers: Artifacts, Size. Below is the python code for it.
import requests
import json
import csv
with open('content.csv', 'w') as csvfile:
headers = ['Artifacts', 'Size']
writer = csv.writer(csvfile)
writer.writerow(headers)
with open('file_list.txt') as file:
for line in file:
url = "http://fqdn/repository/{0}/{1}?describe=json".format(repo_name, line.strip())
response = requests.get(url)
json_data = response.text
data = json.loads(json_data)
for size in data['items']:
if size['name'] == 'Payload':
value_size= size['value']['Size']
if value_size != -1:
with open('content.csv', 'a') as fileappend:
data_writer = csv.writer(fileappend)
data_writer.writerow({line.strip(), str(value_size)})
Issue I am stuck with is, I am not seeing the csv file as expected with Artifacts column with artifact names and Size column with their sizes, instead I am seeing mixed pattern, where some of the rows are correct and some of the rows shows size first and artifact next.
Another issue(not so important), is an added empty row gap between each line when opened in Excel.
Sample CSV data
Artifacts,Size
3369,mysql.odbc/5.1.14
641361,curl/7.24.0
2142246,curl/7.24.0.20120225
2163958,curl/7.25.0
curl/7.55.0,3990517
curl/7.55.1,3991943
3875614,curl/7.54.1
curl/7.58.0,3690457
putty.portable/0.67,4201
6227,notepadplusplus/7.5.4
4407,openjdk8/8.242.8.1
5453,dotnetcore-sdk/3.1.201
4405,openjdk8/8.252.9
Any help is much appreciated.
CodePudding user response:
you're sending a set
as a parameter to write_row()
.
set
object is hash-based and therefore doesn't necessarily keep the order.
you should use a list
or a tuple
instead.
in your last row:
# replace curly brackets with square brackets
data_writer.writerow([line.strip(), str(value_size)])