Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSV file extracting

I am struggling with troubleshooting this problem.

I have a csv file that looks like this:

Elias,"Elias won 0.0% of all there games!
[37mThere score: 0  |  Robots Score: 1"

I am trying to import the number ‘0’ and ‘1’ into a variable however these numbers are always changing so i want some kind of code that can grab the value of it no matter what it is thats there.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example, if the file looked like :

Andrew,"Andrew won 13.5% of all there games!
[37mThere score: 6  |  Robots Score: 17"

I would need to extract the 6 and the 17

Im importing into python

I tried doing

import csv

# Open the CSV file
with open('your_file.csv', newline='') as csvfile:
    # Create a CSV reader object
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')

    # Loop through each row of the CSV file
    for row in reader:
        # Extract the number from the row
        number = row[0].split(': ')[-1].split()[0]
        # Convert the number to a float (if desired)
        number = float(number)
        # Print the number to verify it was extracted correctly
        print(number)

however this gave me the wrong output

>Solution :

You have to:

  • first split by |
  • split by :
  • get last element
  • remove whitespaces
  • cast to int
import csv

with open('your_file.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')

    for row in reader:
        numbers =[int(scores.split(":")[-1].strip()) for scores in row[1].split("|")]
        print(f"{numbers=}") # This is gonna print: numbers=[6, 17]

Side notes:

Option 2

For this kind of operations (stacking one after the other) it’s sometimes convenient to split it into particular steps using generator expressions. They are lazily evaluated, so no matter how big file you are processing you’re not gonna run out of memory and you can see each particular step as a single line which is easier imho to analyze (instead of a single long list comprehension).

import csv

with open('your_file.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')

    for row in reader:
        numbers = (scores.split(":") for scores in row[1].split("|"))
        numbers = (scores[-1] for scores in numbers)
        numbers = (scores.strip() for scores in numbers)
        numbers = (int(scores) for scores in numbers)
        # The you can loop through the numbers
        for n in numbers:
            print(f"{n=}") # This is gonna print: n=6 in the 1st iteration and n=17 in the 2nd
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading