A
A
Alexander Prokopenko2021-08-05 11:32:00
Python
Alexander Prokopenko, 2021-08-05 11:32:00

How to find all rows under another one?

In general, I need to make a graph from the data in the document. That is, I need to find all the data under the symbol X and Y, and then write them to json. Well, there are no problems with json, but how to find all the words under the string: "X" I have no idea. If someone can help I will be very happy!

the data in the document looks something like this:

XY
1 1
2 2
3 3
4 4
5 5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-08-05
@Goverl

Well, if this is the format:

import csv, io, json

data = '''X Y
1 1
2 2
3 3
4 4
5 5'''
f = io.StringIO(data)
reader = csv.DictReader(f, delimiter=' ')
json.dumps(list(reader))

# '[{"X": "1", "Y": "1"}, {"X": "2", "Y": "2"}, {"X": "3", "Y": "3"}, {"X": "4", "Y": "4"}, {"X": "5", "Y": "5"}]'

instead of using io - in your case, you can just read from a file, of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question