Isolate a column from a text file in Python

victor.lecomte -  
 Anonymous user -
Hello,

I have a text file, and I would like to extract a column, below is the original file:

5g8hj;Tournevis;cm;5,10
6r9tl;Marteau;cm;8,20
5d6ft;peinture;l;50,10
7ja3t;Vis 12*12,25;cm;500,001
2gh6t;Clé BTR;mm;25,15
56ml1;Scie;m2;65,12
12654;Pince;m;12*12,12
56ml1;Scie à bois;m2;65,12
12654;Planche;m;12*12,12

And I would like to obtain the following result in another file:

cm
l
cm
cm
mm
m2
m
m2
m

I have seen lots of examples but they didn't match what I wanted so could you help me

2 answers

  1. jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   4 832
     
    Hello,

    I would be curious to see what you may have tried and what doesn’t work as you’d hoped... There are plenty of examples online for reading CSV files or even just for splitting strings.

    For example:
    https://www.google.com/search?q=python+read+csv+file
    https://www.google.com/search?q=python+string+split

    Best regards,
    Jordane
    1
  2. Anonymous user
     
    Hello

    if you are strict, you cannot get what you want
    There’s no l in the 2nd line and there’s no cm in the 3rd, it’s the opposite.

    That aside. Your starting file is a file separated by ; (a Microsoft csv) and you want to retrieve the 3rd piece of information from each line.

    I don’t know how to write it in Python but the principle is simple:
    • split the line using ; as delimiter (that’s called split)
    • retrieve the 3rd item of the split.


    And even with a bit of luck, in Python there’s already something that does that, and it would be called a CSV Parser
    for example
    https://realpython.com/python-csv/
    When I was little, the sea Morte was only sick.
    George Burns
    1
    1. Anonymous user
       
      Hi Jordane
      0