CSV file extracting

Question:

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.

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

Asked By: Elias Khoury

||

Answers:

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
Answered By: Gameplay
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.