python如何读取文本中的数据并处理_Python中基本的读文件和简单数据处理

DataQuest上面的免费课程(本文是Python基础课程部分),里面有些很基础的东西(csv文件读,字符串预处理等),发在这里做记录。涉及下面六个案例:

Find the lowest crime rate(读取csv文件,字符串切分,for循环和if判断过滤数据)

Discover weather pattern in LA(for循环和if判断进行频数统计)

Building a Spell Checker(词频统计,字符串预处理,字典跑字符串,统计正确错误单词)

Analyze NFL data(使用CSVmodule导入文件,类,函数,使用字典和list进行简单统计)

What should you name your kid if you want them to be a US Congressperson?(数据预处理,强制类型转换int(),try-except语句,字典方式统计,转存需要数据)

Which airline is delayed the most?

附录:逐行读取txt文件

案例1 Find the lowest crime rate

(读取csv文件,字符串切分,for循环和if判断过滤数据)

crime_rates.csv是单sheet,73Rows,2Cols的文件。第一列是城市名称(字符串),第二列是犯罪数量(整数)。但是读入Python开始都是字符串,在后面类型转换将字符串形式的犯罪数量强制转换成整型。 并将分隔开转换后的数据存到full_data这个list中,然后使用for循环将犯罪数量最小的城市找出来(if判断,已知犯罪数最小为130),并将这个城市名存入变量city中。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

# We know that the lowest crime rate is 130.

# This is the second column of the data.

# We need to find the corresponding value in the first column -- the city with the lowest crime rate.

# Let's load the csv file

f=open('crime_rates.csv','r')

data=f.read()

rows=data.split('\n')

full_data=[]

forrow inrows:

split_row=row.split(",")

split_row[1]=int(split_row[1])

full_data.append(split_row)

city=""

lowest_crime_rate=10000

foritem infull_data:

ifitem[1]==130:

city=item[0]

案例2 Discover weather pattern in LA

(for循环和if判断进行频数统计)

两列数据的文本文件,有表头。导入la_weather.txt文本文件,切分,存入变量weather_data中,去掉表头。使用字典(dictionary)进行不同类型的频数统计。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

weather_data=[]

f=open("la_weather.csv",'r')

data=f.read()

rows=data.split('\n')

forrow inrows:

split_row=row.split(",")

weather_data.append(split_row)

print(weather_data)

#去掉表头

weather=weather_data[1:367]

weather_counts={}

foritem inweather:

ifitem inweather_counts:

weather_counts[item]=weather_counts[item]+1

else:

weather_counts[item]=1

print(weather_counts)

案例3 Building a Spell Checker

(词频统计,字符串预处理,字典跑字符串,统计正确错误单词)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

# 将字符正规化,对字符进行处理,去掉特殊符号

defnormalize(token):

token=token.replace(".","")

token=token.replace(",","")

token=token.replace("'","")

token=token.replace(";","")

token=token.replace("\n","")

token=token.lower()

returntoken

# 建立一个list用于存放正规的字典

normalized_dictionary_tokens=[]

# 只读方式打开一个文件

f=open("dictionary.txt","r")

raw_data=f.read()

# 按照空格将字符串进行切分,成单个单词

data=raw_data.split(" ")

# 遍历切分后的单词,进行正规化处理(def normalize,去掉特殊符号)

fortokenindata:

normalized_dictionary_tokens.append(normalize(token))

print(normalized_dictionary_tokens)

#统计正确单词和错误单词的词频。用一个正确单词的字典来遍历这个字符串,并进行统计

potential_misspellings=[]

correctly_spelled=[]

fortokeninnormalized_story_tokens:

iftokeninnormalized_dictionary_tokens:

correctly_spelled.append(token)

else:

potential_misspellings.append(token)

print(correctly_spelled)

print(potential_misspellings)

案例4 Analyze NFL data

(使用CSVmodule导入文件,类,函数,使用字典和list进行简单统计)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

importcsv

classTeam():

def__init__(self,name):

self.name=name

f=open("nfl.csv",'r')

csvreader=csv.reader(f)

self.nfl=list(csvreader)

defcount_total_wins(self):

count=0

forrow inself.nfl:

ifrow[2]==self.name:

count=count+1

returncount

defwins_by_years(self):

wins={}

years=["2009","2010","2011","2012","2013"]

foryear inyears:

count=0

forrow inself.nfl:

ifrow[2]==self.name androw[0]==year:

count+=1

wins[year]=count

returnwins

niners=Team("San Francisco 49ers")

niners_wins_by_year=niners.wins_by_years()

print("Niners_wins_by_year: ",niners_wins_by_year)

案例5 What should you name your kid if you want them to be a US Congressperson?

(数据预处理,强制类型转换int(),try-except语句,字典方式统计,转存需要数据)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

# legislators变量是一个2维list,大list里的其中一个list(条目)是一个有7个元素组成的(姓,名,出生年月日,未知,未知,未知)。我们要做的是这一组数据进行预处理,然后进行姓名的统计。

genders_list=[]

unique_genders=set()

unique_genders_list=[]

# 将性别数据以append方式挨个读入list变量genders_list中去

forrow inlegislators:

genders_list.append(row[3])

# genders_list变量使用set()函数进行元素去重变为字典,并存入字典变量unique_genders中,将去重后的结果再存储成list类型数据搭配到变量unique_genders_list

unique_genders=set(genders_list)

unique_genders_list=list(unique_genders)

print(genders_list)

# 已知性别数据的错误值为"",将其重赋值为“M”

forrow inlegislators:

ifrow[3]=="":

row[3]="M"

# 统计出生年份存入list变量birth_years中。其中需要使用split方法对list中的某个元素进行切分,取其中第一个元素(即年),以append追加的方法存入list变量birth_years中

birth_years=[]

forrow inlegislators:

birth_list=[]

birth_list=row[2].split("-")

birth_years.append(birth_list[0])

# 对list变量进行enumerate()函数操作(得到下标和所在的当前row)类似对字典进行.item()方法(得到key和对应的value)。

# 将年份存入list变量legislators中每行的第八列,按照append追加的方法

fori,row inenumerate(legislators):

row.append(birth_years[i])

# 将legislatros变量的第八列元素(出生年份)的字符串类型,强制类型转换成int类型。如遇到强制转换错误就将出生年份值变为0

forrow inlegislators:

try:

row[7]=int(row[7])

exceptException:

row[7]=0

# 用字典进行姓名统计(key为姓名,value为出现次数)存入male_name_counts字典变量中。并将出现次数最多的名字(同样是最大出现次数,但名字不止一个),将这些名字存入list变量top_male_names中

top_male_names=[]

male_name_counts={}

# 用字典进行姓名统计,条件是出生年份大于1940,并且是女性

forrow inlegislators:

ifrow[7]>1940androw[3]=="M":

ifrow[1]inmale_name_counts:

male_name_counts[row[1]]+=1

else:

male_name_counts[row[1]]=1

# 找出名字出现最多的次数highest_value

highest_value=None

forkey,value inmale_name_counts.items():

ifhighest_value isNoneorvalue>highest_value:

highest_value=value

# 将名字次数出现最多的名字(同样是最大出现次数,但名字不止一个),将这些名字以追加append的方式存入list变量top_male_names中

forkey,value inmale_name_counts.items():

ifvalue==highest_value:

top_male_names.append(key)

案例6 Which airline is delayed the most?

这个案例来来回回做了好几天,反正基本上大都是参考答案做过的……酱油了……

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

defcolumn_number_from_name(column_name):

column_number=None

fori,column inenumerate(column_names):

ifcolumn==column_name:

column_number=i

returncolumn_number

deffind_average_delay(carrier_name=None):

total_delayed_flights=0

total_delay_time=0

delay_time_column=column_number_from_name("arr_delay")

delay_number_column=column_number_from_name("arr_del15")

carrier_column=column_number_from_name("carrier")

forrow inflight_delays:

ifcarrier_name isNoneorrow[carrier_column]==carrier_name:

total_delayed_flights+=float(row[delay_number_column])

total_delay_time+=float(row[delay_time_column])

returntotal_delay_time/total_delayed_flights

delays_by_carrier={}

carrier_column=column_number_from_name("carrier")

carriers=[row[carrier_column]forrow inflight_delays]

unique_carriers=list(set(carriers))

forcarrier inunique_carriers:

delays_by_carrier[carrier]=find_average_delay(carrier)

print(delays_by_carrier)

附录1 逐行读取txt文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

# 方法一

f=open("foo.txt")# 返回一个文件对象

line=f.readline()# 调用文件的 readline()方法

whileline:

print line,# 后面跟 ',' 将忽略换行符

# print(line, end = '')   # 在 Python 3中使用

line=f.readline()

f.close()

# 方法二

forline inopen("foo.txt"):

print line

# 方法三

f=open("c:\\1.txt","r")

lines=f.readlines()#读取全部内容

forline inlines

print line

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值