pandas 操作csv文件时,一直报错,排查后发现csv文本中存在很多“空行”:

So 需要把空行全部去掉:

def clearBlankLine():
file1 = open('text1.txt', 'r', encoding='utf-8') # 要去掉空行的文件
file2 = open('text2.txt', 'w', encoding='utf-8') # 生成没有空行的文件
try:
for line in file1.readlines():
if line == '\n':
line = line.strip("\n")
file2.write(line)
finally:
file1.close()
file2.close() if __name__ == '__main__':
clearBlankLine()

根据上面代码即可实现。

补充:

  代码中用都是 open(), 可改为 with open()as  f:  形式,省去手动 close。

Python 去掉文本中空行的更多相关文章

  1. python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  2. python去除文本中的HTML标签

    def SplitHtmlTag(file): with open(file,"r") as f,open("result.txt","w+" ...

  3. 使用dreamweaver去掉文本中的空格和换行

    当我们从其他地方拷贝文本到网页,在html代码中会自动带有空格和换行,手动去掉很麻烦,今天试着用dreamweaver去了一下,方法如下: 1.点击Ctrl+F,打开“查找和替换”窗口 2‘见下图:

  4. Shell 去掉文本中的空格

    使用sed命令 将文件'aol1'中的空格去掉然后输出到'tmpFile'文件中 sed s/[[:space:]]//g aol1 > tmpFile

  5. python从文本中提取某酒店机顶盒号和智能卡号

    1.某项目中经常遇到需要关闭一些机顶盒消费权限.但是给过来的不是纯字符串,需要自己提取. 有400多个机顶盒和智能卡.nodepad++的列块模式也可以提取,但是还是稍微麻烦,因为列不对等 先复制到文 ...

  6. python 过滤文本中的标点符号(转)

    网上搜到的大都太复杂,最后找到一个用正则表达式实现的: import re s = "string. With. Punctuation?" # 如果空白符也需要过滤,使用 r'[ ...

  7. bash python获取文本中每个字符出现的次数

    bash: grep -o . myfile | sort |uniq -c python:  使用collections模块 import pprint import collections f = ...

  8. python去掉字符串中空格的方法

    1.strip():把头和尾的空格去掉 2.lstrip():把左边的空格去掉 3.rstrip():把右边的空格去掉 4.replace('c1','c2'):把字符串里的c1替换成c2.故可以用r ...

  9. 使用python读取文本中结构化数据

    需求 read some .txt file in dir and find min and max num in file. solution: echo *.txt > file.name ...

随机推荐

  1. God web

    http://my.csdn.net/yerenyuan_pku http://evilcos.me/?p=336 https://www.zhihu.com/question/21606800 st ...

  2. Linux制作wifi热点/无线路由

    参考: http://blog.csdn.net/u011641885/article/details/495121991.工具/原料    有无线网卡(usb接口的RT3070无线网卡).有线网卡的 ...

  3. Mtx——Mobile Tutorial Series (LibGDX & MTX)

    http://moribitotechx.blogspot.co.uk/p/tutorial-series-libgdx-mtx.html —————————————————————————————— ...

  4. google pr值查询接口

    PR(全称PageRank)是Google衡量一个网站的重要标准之一,从而影响Google搜索结果排名.Google官方提供了查询PR的API,如本站PR值:http://toolbarqueries ...

  5. Swift-7-闭包

    // Playground - noun: a place where people can play import UIKit // swift 中闭包与C和OC中的blocks比较相似 // 1. ...

  6. Install EPEL repo on CentOS 7 / RHEL 7

    On CentOS 7, we have found without downloading the epel-release RPM package(as we used to do on prev ...

  7. 【统计分析】ROC曲线

    http://baike.baidu.com/link?url=O8nVf39qW4UpYAegk9cJfYARCFDg8YHQ6p5wFnWxYvo151doXo-WvG5Rfz0j4R-r 受试者 ...

  8. asp.net调用系统设置字体文本框的方法

    本文实例展示了asp.net调用系统设置字体文本框的方法,是进行web开发中很实用的技巧.具体实现步骤如下: 一.调用系统字体文本框 首先在bin文件夹右击-->添加引用-->.net标签 ...

  9. SurvivalShooter学习笔记(八.敌人管理器)

    敌人管理器:管理敌人的随机出生点创建 在场景中建立几个空物体,作为敌人的出生点 public class EnemyManager : MonoBehaviour { public PlayerHea ...

  10. Eclipse Android 代码自己主动提示功能

    Eclipse Android 代码自己主动提示功能 Eclipse for android 实现代码自己主动提示智能提示功能.介绍 Eclipse for android 编辑器中实现两种主要文件 ...