Python从CSV文件中读取数据集

huangapple go评论86阅读模式
英文:

Python Read dataset from CSV

问题

我已经完成了一个小项目,但不知道如何从CSV数据集文件中读取数据,并且不知道如何按照描述返回这些值,希望你能帮助我。再次万分感谢。我在以下链接的Google Drive中附上了数据集文件和描述。

链接:https://drive.google.com/file/d/16fU_g9jevu-A6Laxs1zeeZNzjGW73KQ4/view?usp=sharing

英文:

I have done a small project but do not know how to read from the csv dataset file and how to return the values ​​as in the description, I hope you can help me.
Once again many thanks.I was attached link google drive contain the file dataset and description.

https://drive.google.com/file/d/16fU_g9jevu-A6Laxs1zeeZNzjGW73KQ4/view?usp=sharing

答案1

得分: 1

你可以使用Python的"Pandas"框架:

import pandas as pd

f = "路径/到/你的/文件.csv"
dataframe = pd.read_csv(f)
# 处理你的数据

Pandas为你提供了许多功能来处理和管理数据集。

或者你可以使用标准的CSV写入方法。

import csv

file = "路径/到/你的/文件.csv"
with open(file, "r") as f:
    reader = csv.reader(f)

    for row in reader:
    # 处理你的数据
英文:

You can use the python "Pandas" framework:

import pandas as pd

f = "path/to/your/file.csv"
dataframe = pd.read_csv(f)
# handle your data

Pandas gives you a lot of functionalities to handle and manage the dataset.

Or you can use the standard csv writer.

import csv

file = "path/to/your/file.csv"
with open(file, "r") as f:
    reader = csv.reader(f)

    for row in reader:
    # handle your data

答案2

得分: 0

尝试这个
```python
import csv
with open('myfile') as file:
    reader = csv.reader(file)
    for details in reader:
        #检索你的数据

我之前也需要做类似的事情,但涉及电子邮件和姓名。我有一个像这样的 csv 文件:
pic of csv file

然后我使用了这段代码

import csv
with open("emails.csv") as file:
    reader = csv.reader(file)
    next(reader)
    for name, email in reader:
        #在这里给每个人发送个性化的电子邮件

<details>
<summary>英文:</summary>

Try This
```python
import csv
with open(&#39;myfile&#39;) as file:
    reader = csv.reader(file)
    for details in reader:
        #retrieve your data

I had to do something similar before but with emails and names. I had a csv like this:
pic of csv file

Then I used this code

import csv
with open(&quot;emails.csv&quot;) as file:
    reader = csv.reader(file)
    next(reader)
    for name, email in reader:
        #sent each person a personalized email here

huangapple
  • 本文由 发表于 2020年8月25日 22:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63580927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定