当前位置:
首页 > 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
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数