I am tryin to parse a string from screeninfo to be useable in a csv file. here is my code
JavaScript
x
9
1
import csv
2
from screeninfo import get_monitors
3
4
with open('MultiMonitor.csv', 'w') as csvfile:
5
csvwriter = csv.writer(csvfile)
6
for m in get_monitors():
7
8
csvwriter.writerow([(str(m))])
9
here is the output:
JavaScript
1
3
1
Monitor(x=0, y=0, width=3840, height=2160, width_mm=345, height_mm=194, name='\\.\DISPLAY1')
2
3
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
JavaScript
1
8
1
import csv
2
from screeninfo import get_monitors
3
4
with open('MultiMonitor.csv', 'w') as csvfile:
5
csvwriter = csv.writer(csvfile)
6
for m in get_monitors():
7
csvwriter.writerow(['monitor', m.x, my.x, m.width, m.height, m.name])
8
and if you want with x=
, y=
then you can format line as string and write without csvwriter
JavaScript
1
8
1
from screeninfo import get_monitors
2
3
with open('MultiMonitor.csv', 'w') as f:
4
for m in get_monitors():
5
text = f'monitor, x={m.x}, y={m.y}, width={m.width}, height={m.height}, name={m.name}'
6
#print(text)
7
f.write(text + 'n')
8