Python Forum
writing data from Amazon Workspaces to CSV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing data from Amazon Workspaces to CSV
#3
(Sep-22-2022, 10:46 PM)isaac_python Wrote: Hello,

I'm trying to get my output from Amazon Workspaces to print to a file. I can get the data from workspaces, but I have not been able to write the data to the CSV file. I have been looking around the web, and all the answers I can find are the simplest possible answers, and only print the code from within Python itself. I can find no real-world examples that adequately address my problem. Any assistance is appreciated, Thank you.

This is my code:
[python]import boto3
client = boto3.client('workspaces')
folder = input('directoryId> ')

workspaces = client.describe_workspaces()['Workspaces']
directoryIds = [workspaces['DirectoryId']for workspaces in workspaces]
directoryIds.sort()


for DirectoryIds in folder:
    print(workspaces)

    
   
##----------------CSV Writer Code----------------##


import csv
with open('workspacesdata.csv', 'w', newline='') as csvfile:
    datawriter = csv.writer(csvfile,dialect='excel')
    
    fieldnames=['WorkspaceId', 'DirectoryId', 'UserName', 'IpAddress','State', 'BundleId', 'SubnetId','ComputerName','VolumeEncryptionKey' , 'WorkspaceProperties','RunningMode']
    datawriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
    datawriter.writeheader()
  


##----------------CSV Reader Code----------------##

import os
os.system('start excel.exe workspacesdata.csv')
I should have probably mentioned I am new to using python, so I expect to make mistakes and I realized I have code that may not work.

As requested here is a snippet of the raw output that I want in my file:

Output:
'ComputeTypeName': 'STANDARD'}, 'ModificationStates': []}, {'WorkspaceId': 'ws-2ptlkm5qv', 'DirectoryId': 'd-92671ce35c', 'UserName': 'mdomi077', 'IpAddress': '10.113.91.208', 'State': 'AVAILABLE', 'BundleId': 'wsb-9mw5qstcj', 'SubnetId': 'subnet-055c1fd5f0f78926a', 'ComputerName': 'EC2AMAZ-T18AKN8', 'VolumeEncryptionKey': 'arn:aws:kms:us-west-2:336694646358:key/c0bf7254-4ed0-49db-b068-aa35aead1569', 'UserVolumeEncryptionEnabled': True, 'RootVolumeEncryptionEnabled': True, 'WorkspaceProperties': {'RunningMode': 'ALWAYS_ON', 'RootVolumeSizeGib': 80, 'UserVolumeSizeGib': 10, 'ComputeTypeName': 'STANDARD'}, 'ModificationStates': []}, {'WorkspaceId': 'ws-6bw7svsnp', 'DirectoryId': 'd-92672c445d', 'UserName': 'arico027', 'IpAddress': '10.159.250.248', 'State': 'AVAILABLE', 'BundleId': 'wsb-b31hkn0hd', 'SubnetId': 'subnet-0bd2f24ad8422306f', 'ComputerName': 'WSAMZN-T8VRNFTT', 'VolumeEncryptionKey': 'arn:aws:kms:us-west-2:336694646358:key/95d05f8e-7e21-4852-8cb8-f4a8505d8110', 'UserVolumeEncryptionEnabled': True, 'RootVolumeEncryptionEnabled': True, 'WorkspaceProperties': {'RunningMode': 'ALWAYS_ON', 'RootVolumeSizeGib': 80, 'UserVolumeSizeGib': 10, 'ComputeTypeName': 'STANDARD'}, 'ModificationStates': []}, {'WorkspaceId': 'ws-08h9cjkvt', 'DirectoryId': 'd-92670ed06d', 'UserName': 'vsanc079', 'IpAddress': '10.113.81.161', 'State': 'AVAILABLE', 'BundleId': 'wsb-35gdsrk4x', 'SubnetId': 'subnet-04f8860f4dbcbb4e4', 'ComputerName': 'WSAMZN-J6IHDJ93', 'VolumeEncryptionKey': 'arn:aws:kms:us-west-2:336694646358:key/bef5e0aa-2c8f-4b2d-8c31-9456e5de18ce', 'UserVolumeEncryptionEnabled': True, 'RootVolumeEncryptionEnabled': True, 'WorkspaceProperties': {'RunningMode': 'ALWAYS_ON', 'RootVolumeSizeGib': 80, 'UserVolumeSizeGib': 10, 'ComputeTypeName': 'STANDARD'}, 'ModificationStates': []}, {'WorkspaceId': 'ws-h6vs896fs', 'DirectoryId': 'd-92672c44a1', 'UserName': 'achoa004', 'IpAddress': '10.159.250.36', 'State': 'AVAILABLE', 'BundleId': 'wsb-gm4d5tx2v', 'SubnetId': 'subnet-0eb119c83fcd876b5', 'ComputerName': 'WSAMZN-JOSCE5IC', 'VolumeEncryptionKey': 'arn:aws:kms:us-west-2:336694646358:key/95d05f8e-7e21-4852-8cb8-f4a8505d8110', 'UserVolumeEncryptionEnabled': True, 'RootVolumeEncryptionEnabled': True, 'WorkspaceProperties': {'RunningMode': 'ALWAYS_ON', 'RootVolumeSizeGib': 80, 'UserVolumeSizeGib': 100, 'ComputeTypeName': 'PERFORMANCE'}, 'ModificationStates': []}, {'WorkspaceId': 'ws-b1t8zwt6b', 'DirectoryId': 'd-926728fc2c', 'UserName': 'drahu001', 'IpAddress': '10.159.24.21', 'State': 'AVAILABLE', 'BundleId': 'wsb-8vbljg4r6', 'SubnetId': 'subnet-0c63e957', 'ComputerName': 'IP-C0A8D2AA', 'WorkspaceProperties': {'RunningMode': 'ALWAYS_ON', 'RootVolumeSizeGib': 80, 'UserVolumeSizeGib': 50, 'ComputeTypeName': 'STANDARD'}, 'ModificationStates': []}]
I have simplified the code and made the suggested adjustments

import boto3
client = boto3.client('workspaces')
folder = input('directoryId> ')

workspaces = client.describe_workspaces()['Workspaces']
directoryIds = [workspaces['DirectoryId']for workspaces in workspaces]
directoryIds.sort()

for DirectoryIds in folder:
    print(workspaces)

    
   
##----------------CSV Writer Code----------------##


import csv
#with open('workspacesdata.csv', 'w', newline='') as csvfile:
with open('workspacesdata.csv', 'w') as fp:
    print('created', workspaces)
    datawriter = csv.writer(csvfile,dialect='excel')
    
    fieldnames=['WorkspaceId', 'DirectoryId', 'UserName', 'IpAddress','State', 'BundleId', 'SubnetId','ComputerName','VolumeEncryptionKey' , 'WorkspaceProperties','RunningMode']
    datawriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
    datawriter.writeheader()
  


##----------------CSV Reader Code----------------##

import os
os.system('start excel.exe workspacesdata.csv')

import os
os.system('start excel.exe workspacesdata.csv')[/color]
Error:
"vpc.py", line 21, in <module> datawriter = csv.writer(csvfile,dialect='excel') NameError: name 'csvfile' is not defined PS D:\Users\username>"
Larz60+ write Sep-23-2022, 04:38 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Made attempt to do this for you. Please use bbcode tags on future posts.
Reply


Messages In This Thread
RE: writing data from Amazon Workspaces to CSV - by isaac_python - Sep-23-2022, 04:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tools for writing and reading binary data in files Skaperen 0 1,481 Jun-08-2020, 07:28 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020