英文:
CSV file extracting
问题
我正在困扰于解决这个问题。
我有一个看起来像这样的 CSV 文件:
ANONYMOUS,"ANONYMOUS 赢得了所有比赛的 0.0%![37m他们的分数: 0 | 机器人的分数: 1"
我尝试将数字 '0' 和 '1' 导入到一个变量中,但是这些数字总是在变化,所以我想要一种可以获取它们的值的代码,无论它们是什么。
例如,如果文件看起来像这样:
ANONYMOUS1,"ANONYMOUS1 赢得了所有比赛的 13.5%![37m他们的分数: 6 | 机器人的分数: 17"
我需要提取出 6 和 17。
我正在使用 Python 进行导入。
我尝试了以下代码:
import csv
# 打开 CSV 文件
with open('your_file.csv', newline='') as csvfile:
# 创建 CSV 读取器对象
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
# 遍历 CSV 文件的每一行
for row in reader:
# 从行中提取数字
number = row[0].split(': ')[-1].split()[0]
# 将数字转换为浮点数(如果需要)
number = float(number)
# 打印数字以验证是否正确提取
print(number)
然而,这给了我错误的输出。
英文:
I am struggling with troubleshooting this problem.
I have a csv file that looks like this:
ANONYMOUS,"ANONYMOUS 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 :
ANONYMOUS1,"ANONYMOUS1 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
答案1
得分: 2
以下是翻译后的代码部分:
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=}") # 这将打印:numbers=[6, 17]
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)
# 然后你可以循环遍历这些数字
for n in numbers:
print(f"{n=}") # 这将在第一次迭代中打印:n=6,在第二次迭代中打印:n=17
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论