VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • DataAnalysis-读取本地数据

一、TXT文件操作

读取全部内容

复制代码
import numpy as np
import pandas as pd
复制代码
txt_filename = './files/python_wiki.txt'

# 打开文件
file_obj = open(txt_filename,'r')

# 读取整个文件内容
all_content = file_obj.read()

# 关闭文件
file_obj.close()

print (all_content)
复制代码
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]
Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]
Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is open source software[30] and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.

逐行读取

复制代码
txt_filename = './files/python_wiki.txt'

# 打开文件
file_obj = open(txt_filename, 'r')

# 逐行读取
line1 = file_obj.readline()
print (line1)
复制代码
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]
复制代码
# 继续读下一行【不会全部读完】
line2 = file_obj.readline()
print (line2)

# 关闭文件
file_obj.close()
复制代码
Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]

读取全部内容,返回列表

复制代码
txt_filename = './files/python_wiki.txt'

# 打开文件
file_obj = open(txt_filename, 'r')

lines = file_obj.readlines()

for i, line in enumerate(lines):
    print ('%i: %s' %(i, line))

# 关闭文件
file_obj.close()
复制代码
0: Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]

1: Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]

2: Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is open source software[30] and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.

写操作

复制代码
txt_filename = './files/test_write.txt'

# 打开文件
file_obj = open(txt_filename, 'w')

# 写入全部内容
file_obj.write("《Python数据分析》")
file_obj.close()

复制代码
txt_filename = './files/test_write.txt'

# 打开文件
file_obj = open(txt_filename, 'w')

# 写入字符串列表
lines = ['这是第%i行\n' %n for n in range(10)]
file_obj.writelines(lines)
file_obj.close()

with语句

复制代码
txt_filename = './files/test_write.txt'
with open(txt_filename, 'r') as f_obj:
    print (f_obj.read())

复制代码
这是第0行
这是第1行
这是第2行
这是第3行
这是第4行
这是第5行
这是第6行
这是第7行
这是第8行
这是第9行

二、CSV文件操作

pandas读csv文件

根据路径导入数据以及指定的列

复制代码
import pandas as pd
filename = './files/presidential_polls.csv'
df = pd.read_csv(filename, usecols=['cycle', 'type', 'startdate'])#导入指定列
print (type(df))
print (df.head())
复制代码
<class 'pandas.core.frame.DataFrame'>
   cycle        type   startdate
0   2016  polls-plus  10/25/2016
1   2016  polls-plus  10/27/2016
2   2016  polls-plus  10/27/2016
3   2016  polls-plus  10/20/2016
4   2016  polls-plus  10/20/2016

引用指定的列

复制代码
cycle_se = df['cycle']
print (type(cycle_se))
print (cycle_se.head())
复制代码
<class 'pandas.core.series.Series'>
0    2016
1    2016
2    2016
3    2016
4    2016
Name: cycle, dtype: int64

多层索引成dataframe类型

复制代码
filename = './files/presidential_polls.csv'
df1 = pd.read_csv(filename,usecols=['cycle', 'type', 'startdate','state','grade'],index_col = ['state','grade'])
print(df1.head())
复制代码
                cycle        type   startdate
state    grade                               
U.S.     B       2016  polls-plus  10/25/2016
         A+      2016  polls-plus  10/27/2016
Virginia A+      2016  polls-plus  10/27/2016
Florida  A       2016  polls-plus  10/20/2016
U.S.     B+      2016  polls-plus  10/20/2016

跳过指定的行

复制代码
filename = './files/presidential_polls.csv'
df2 = pd.read_csv(filename,usecols=['cycle', 'type', 'startdate','state','grade'],skiprows=[1, 2, 3])
print(df2.head())
复制代码
   cycle        type         state   startdate grade
0   2016  polls-plus       Florida  10/20/2016     A
1   2016  polls-plus          U.S.  10/20/2016    B+
2   2016  polls-plus          U.S.  10/22/2016     A
3   2016  polls-plus          U.S.  10/26/2016    A-
4   2016  polls-plus  Pennsylvania  10/25/2016    B-

pandas写csv文件

·to_csv里面的index参数作用?===可能是不要索引的意思。

复制代码
filename = './files/pandas_output.csv'
df.to_csv(filename, index=None)

三、JSON文件操作

json读操作

复制代码
import json

filename = './files/global_temperature.json'
with open(filename, 'r') as f_obj:
    json_data = json.load(f_obj)

# 返回值是dict类型
print (type(json_data))
复制代码
<class 'dict'>
复制代码
print (json_data.keys())
复制代码
dict_keys(['description', 'data'])

json转CSV

复制代码
#print json_data['data'].keys()
print (json_data['data'].values())
复制代码
dict_values(['-0.1247', '-0.0707', '-0.0710', '-0.1481', '-0.2099', '-0.2220', '-0.2101', '-0.2559', '-0.1541', '-0.1032', '-0.3233', '-0.2552', '-0.3079', '-0.3221', '-0.2828', '-0.2279', '-0.0971', '-0.1232', '-0.2578', '-0.1172', '-0.0704', '-0.1471', '-0.2535', '-0.3442', '-0.4240', '-0.2967', '-0.2208', '-0.3767', '-0.4441', '-0.4332', '-0.3862', '-0.4367', '-0.3318', '-0.3205', '-0.1444', '-0.0747', '-0.2979', '-0.3193', '-0.2118', '-0.2082', '-0.2152', '-0.1517', '-0.2318', '-0.2161', '-0.2510', '-0.1464', '-0.0618', '-0.1506', '-0.1749', '-0.2982', '-0.1016', '-0.0714', '-0.1214', '-0.2481', '-0.1075', '-0.1445', '-0.1173', '-0.0204', '-0.0318', '-0.0157', '0.0927', '0.1974', '0.1549', '0.1598', '0.2948', '0.1754', '-0.0013', '-0.0455', '-0.0471', '-0.0550', '-0.1579', '-0.0095', '0.0288', '0.0997', '-0.1118', '-0.1305', '-0.1945', '0.0538', '0.1145', '0.0640', '0.0252', '0.0818', '0.0924', '0.1100', '-0.1461', '-0.0752', '-0.0204', '-0.0112', '-0.0282', '0.0937', '0.0383', '-0.0775', '0.0280', '0.1654', '-0.0698', '0.0060', '-0.0769', '0.1996', '0.1139', '0.2288', '0.2651', '0.3024', '0.1836', '0.3429', '0.1510', '0.1357', '0.2308', '0.3710', '0.3770', '0.2982', '0.4350', '0.4079', '0.2583', '0.2857', '0.3420', '0.4593', '0.3225', '0.5185', '0.6335', '0.4427', '0.4255', '0.5455', '0.6018', '0.6145', '0.5806', '0.6583', '0.6139', '0.6113', '0.5415', '0.6354', '0.7008', '0.5759', '0.6219', '0.6687', '0.7402', '0.8990'])
复制代码
# 转换key
year_str_lst = json_data['data'].keys()
year_lst = [int(year_str) for year_str in year_str_lst]
print (year_lst)
复制代码
[1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015]
复制代码
# 转换value
temp_str_lst = json_data['data'].values()
temp_lst = [float(temp_str) for temp_str in temp_str_lst]
print (temp_lst)
复制代码
[-0.1247, -0.0707, -0.071, -0.1481, -0.2099, -0.222, -0.2101, -0.2559, -0.1541, -0.1032, -0.3233, -0.2552, -0.3079, -0.3221, -0.2828, -0.2279, -0.0971, -0.1232, -0.2578, -0.1172, -0.0704, -0.1471, -0.2535, -0.3442, -0.424, -0.2967, -0.2208, -0.3767, -0.4441, -0.4332, -0.3862, -0.4367, -0.3318, -0.3205, -0.1444, -0.0747, -0.2979, -0.3193, -0.2118, -0.2082, -0.2152, -0.1517, -0.2318, -0.2161, -0.251, -0.1464, -0.0618, -0.1506, -0.1749, -0.2982, -0.1016, -0.0714
      



  

相关教程