首页 > 技术文章 > 利用python 模块读取csv文件信息

wutongyuhou 2017-05-15 22:19 原文

还有一个比较简单的方法

# -*- coding=utf-8 -*-

import pandas as pd
df = pd.read_csv("20170320094630.csv",encoding="gb2312")
print("df is \n",df)
标黄的地方,切记,切记

 

 

 

import unicodecsv

enrollments_filename = '/datasets/ud170/udacity-students/enrollments.csv'

## Longer version of code (replaced with shorter, equivalent version below)

# enrollments = []
# f = open(enrollments_filename, 'rb')
# reader = unicodecsv.DictReader(f)
# for row in reader:
# enrollments.append(row)
# f.close()

with open(enrollments_filename, 'rb') as f:
reader = unicodecsv.DictReader(f)
enrollments = list(reader)

### Write code similar to the above to load the engagement
### and submission data. The data is stored in files with
### the given filenames. Then print the first row of each
### table to make sure that your code works. You can use the
### "Test Run" button to see the output of your code.

engagement_filename = '/datasets/ud170/udacity-students/daily_engagement.csv'
submissions_filename = '/datasets/ud170/udacity-students/project_submissions.csv'
def read_csv(filename):
with open(filename,'rb') as f:
reader = unicodecsv.DictReader(f)
return list(reader)

daily_engagement = read_csv(engagement_filename) # Replace this with your code
print daily_engagement[0]
project_submissions = read_csv(submissions_filename) # Replace this with your code
print project_submissions[0]

推荐阅读