Python从CSV文件中读取数据集

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

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"框架:

  1. import pandas as pd
  2. f = "路径/到/你的/文件.csv"
  3. dataframe = pd.read_csv(f)
  4. # 处理你的数据

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

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

  1. import csv
  2. file = "路径/到/你的/文件.csv"
  3. with open(file, "r") as f:
  4. reader = csv.reader(f)
  5. for row in reader:
  6. # 处理你的数据
英文:

You can use the python "Pandas" framework:

  1. import pandas as pd
  2. f = "path/to/your/file.csv"
  3. dataframe = pd.read_csv(f)
  4. # handle your data

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

Or you can use the standard csv writer.

  1. import csv
  2. file = "path/to/your/file.csv"
  3. with open(file, "r") as f:
  4. reader = csv.reader(f)
  5. for row in reader:
  6. # handle your data

答案2

得分: 0

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

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

然后我使用了这段代码

  1. import csv
  2. with open("emails.csv") as file:
  3. reader = csv.reader(file)
  4. next(reader)
  5. for name, email in reader:
  6. #在这里给每个人发送个性化的电子邮件
  1. <details>
  2. <summary>英文:</summary>
  3. Try This
  4. ```python
  5. import csv
  6. with open(&#39;myfile&#39;) as file:
  7. reader = csv.reader(file)
  8. for details in reader:
  9. #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

  1. import csv
  2. with open(&quot;emails.csv&quot;) as file:
  3. reader = csv.reader(file)
  4. next(reader)
  5. for name, email in reader:
  6. #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:

确定