Skip to content
Advertisement

Parse screeninfo into useable csv

I am tryin to parse a string from screeninfo to be useable in a csv file. here is my code

import csv
from screeninfo import get_monitors

with open('MultiMonitor.csv', 'w') as csvfile:
    csvwriter = csv.writer(csvfile)
    for m in get_monitors():

        csvwriter.writerow([(str(m))])

here is the output:

Monitor(x=0, y=0, width=3840, height=2160, width_mm=345, height_mm=194, name='\\.\DISPLAY1')

This results in a one cell csv because it imports the whole string.

I am trying to parse it so that I can call x, y , width, height and display as individual cells.

so it would look similar to

monitor, x=0, y=0, x1=0, y1=0, displayname,

please and thank you.

Advertisement

Answer

I don’t understand why you convert to string str(m) and try to parse it

You can get every value directly m.x, my.x, m.width, m.height, m.name

import csv
from screeninfo import get_monitors

with open('MultiMonitor.csv', 'w') as csvfile:
    csvwriter = csv.writer(csvfile)
    for m in get_monitors():
        csvwriter.writerow(['monitor', m.x, my.x, m.width, m.height, m.name])

and if you want with x=, y= then you can format line as string and write without csvwriter

from screeninfo import get_monitors

with open('MultiMonitor.csv', 'w') as f:
    for m in get_monitors():
        text = f'monitor, x={m.x}, y={m.y}, width={m.width}, height={m.height}, name={m.name}'
        #print(text)
        f.write(text + 'n')
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement