Python读取与存储文件内容

一、.csv文件

读取:

import pandas as pd
souce_data = pd.read_csv(File_Path)

  其中File_path是文件的路径

储存:

import pandas as pd
souce_data.to_csv(file_path)

其中,souce_data格式应该为series或者Dataframe格式

二、Excel文件

读取:

import xlrd as xl
data_excel = xlrd.open_workbook(file_path)
souce_data = data_excel.sheet_by_name(sheet)
row_len = souce_data.nrows
col_len = souce_data.ncols
for i in range(row_len):
for j in range(col_len):
print(souce_data.cell_value(i,j))

  其中,open_workbook(file_path)函数是打开文件file_path,data_excel.sheet_by_name(sheet)函数是打开sheet中的文件并赋值给souce_data。souce_data.nrows与souce_data.ncols是分别计算表格的行数与列数。

三、txt文件

读取:  

Python对txt的内容读取有三类方法:read()、readline()、readlines(),这三种方法各有利弊,下面逐一介绍其使用方法和利弊。

1.read():

  read()函数通过一次性读取文件的所有内容放在一个大字符串中,即存在内存中

with open(file_path) as f:
souce_data = f.read()
print(souce_data)

    read()的优势:方便、简单;一次性独读出文件放在一个大字符串中,速度最快。

    read()的弊端:文件过大的时候,占用内存会过大

2.readline():

  readline()逐行读取文本,结果是一个list

 with open(file_path) as f:
line = f.readline()
while line:
print(line)
line = f.readline()

    readline()的优势:占用内存小,逐行读取。

    readline()的弊端:由于是逐行读取,读取速度比较慢

3.readlines():

  readlines()一次性读取文本的所有内容,结果是一个list

with open(file) as f:
for line in f.readlines():
print line

  这种方法读取的文本内容,每行文本末尾都会带一个'\n'换行符 (可以使用L.rstrip('\n')去掉换行符

    readlines()的利端:一次性读取文本内容,速度比较快

    readlines()的弊端:随着文本的增大,占用内存会越来越多

储存:

with open(file_path,'w') as f:
f.write(souce_data)

四、储存与读取json文件

存储:

import json
with open(file_path,'w') as cf:
cf.write(json.dumps(souce_data))

读取:

import json
with open(file_path,'r') as rf:
souce_data = rf.read()
souce_data = eval(souce_data)

Python读取文件内容与存储的更多相关文章

  1. [python] - 读取文件内容,并输出

    1.读取文件,并逐行输出内容,代码如下: # coding=gbk import os path = 'E:\python_practice' os.chdir(path) fname = raw_i ...

  2. Python读取文件内容的三种方式并比较

    本次实验的文件是一个60M的文件,共计392660行内容. 程序一: def one(): start = time.clock() fo = open(file,'r') fc = fo.readl ...

  3. Python读取文件内容并将内容插入到SSDB中

    import os import linecache import time from SSDB import SSDB ssdb = SSDB('127.0.0.1', 8888) print(&q ...

  4. python读取文件内容方法

    1) readline 每次读一行,返回序列 2) readlines 一次全部读出,返回序列 3) numpy 的genfromtxt,返回为np的矩阵格式 import numpy as np f ...

  5. Python跳过第一行读取文件内容

    Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...

  6. Python读取文件编码及内容

    Python读取文件编码及内容 最近做一个项目,需要读取文件内容,但是文件的编码方式有可能都不一样.有的使用GBK,有的使用UTF8.所以在不正确读取的时候会出现如下错误: UnicodeDecode ...

  7. python练习六十一:文件处理,读取文件内容

    python练习六十一:文件处理,读取文件内容 假设要读取text.txt文件中内容 写文件(如果有文件,那直接调用就行,我这里自己先创建的文件) list1 = ['python','jave',' ...

  8. python读取文件指定行内容

    python读取文件指定行内容 import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeo ...

  9. python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib

    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...

随机推荐

  1. Convolutional Neural Network Architectures for Matching Natural Language Sentences

    interaction  n. 互动;一起活动;合作;互相影响 capture vt.俘获;夺取;夺得;引起(注意.想像.兴趣)n.捕获;占领;捕获物;[计算机]捕捉 hence  adv. 从此;因 ...

  2. Python之简单验证码实现

    def v_code(): ret = '' for i in range(5): num = random.randint(0,9) alf = chr(random.randint(65,122) ...

  3. java-15习题

    通过键盘分别输入年份.月份.日把它存储到日期时间对象中,然后计算1000天以后的日期并输出. import java.util.Calendar; import java.util.Scanner; ...

  4. Array.sort()

    sort() : 是对数组的元素进行排序,并返回一个数组.默认排序方式是根据字符串的Unicode码表的码点. 由于取决于具体实现,所以无法保证它的时间和空间复杂度. arr.sort(compare ...

  5. vue配置 请求本地json数据

    第一步:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require('portfinder')后添加 //第一步const expres ...

  6. Function program language

    历史 Lambda演算为描述函数及其评估提供了理论框架.它是一种数学抽象而不是编程语言 - 但它构成了几乎所有当前函数式编程语言的基础.等效的理论公式,组合逻辑,通常被认为比lambda演算更抽象,并 ...

  7. C# 代码补全

    cw + Tab + Tab           输出 Console.WriteLine(); try +Tab+Tab           输出 try catch代码块 foreach + Ta ...

  8. ES6的一些知识学习

    一.基础 ES6 - 类: class A{ constructor(name,color){ this.name = name; this.color = color; } toString(){ ...

  9. [Tom and Bag][需要记录过程的dp]

    http://acm.beihua.edu.cn/problem/1007 Tom and Bag   Description Tom is the most handsome CCPC contes ...

  10. anki_vector SDK源码解析(教程)

    一:最近anki vector robot开放了Python SDK,我听到的第一时间就赶快上网查了查,先抛几个官网重要链接吧: Python编程API手册及环境搭建等: https://sdk-re ...