Python Forum
How to solve this simple problem? Check if cvs first element is the same in each row? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to solve this simple problem? Check if cvs first element is the same in each row? (/thread-37468.html)



How to solve this simple problem? Check if cvs first element is the same in each row? - thesquid - Jun-14-2022

Ok so I have a cvs with these information (I am using pipes and not commas):

1 | A | B
1 | C | D
2 | E | F


I need a code that does this:
If the first element of the row is the same as first element in the row above or below, then print that first element. I need this only for the first occurrence.

So in the case of 1 | A | B it should print "1" because it is the first occurrence in the cvs file. In case of the second row 1 | C | D it should do nothing because it's the second occurrence.

I don't know how to solve this. I know how to read the cvs file and get the elements.


RE: How to solve this simple problem? Check if cvs first element is the same in each row? - deanhystad - Jun-14-2022

What should be the output of your test case?


RE: How to solve this simple problem? Check if cvs first element is the same in each row? - thesquid - Jun-14-2022

Pretty much the output I want is this:

<h1> 1 </h1>
<h2> A </h2>
B
<h2> C </h2>
D

<h1> 2 </h1>
<h2> D </h2>
E


So the first column is the H1 tag. However if there are more than 1 columns with the same info (for example "1" in our case) then just add the second and 3rd columns as h2 and content.
The most important thing is that there should no be two h1 tags with the same content.