import csv

def get_lines(filepath):
with open(filepath) as file_object:
lines=set(file_object.readlines())
return lines def new_csv(lines):
fileindex=0
count=len(lines)
print("总行数"+str(count))
for index,line in enumerate(lines):
index+=1
#print(str(index)+'_'+line)
oneline=line.strip()#逐行读取,剔除空白
if (index-1) % 1000 +1 ==1:
data=[]
if len(oneline)==11:
data.append([oneline])
elif index % 1000 ==0 or index==count:
fileindex+=1
if len(oneline)==11:
data.append([oneline])
with open(str(fileindex)+'.csv','w') as csvfile:
csv_writer=csv.writer(csvfile,dialect='excel')
#csv_writer=csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerows(data)
else:
if len(oneline)==11:
data.append([oneline])
if __name__ == "__main__":
filepath="Text.txt"
lines=get_lines(filepath)
new_csv(lines)

  

20171026python读取txt写入csv的更多相关文章

  1. python读取与写入csv,txt格式文件

    python读取与写入csv,txt格式文件 在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中.将csv文件中的数据直接读取为dict类型和DataFrame是非常方便也很 ...

  2. python读取和写入csv文件

    读取csv文件: def readCsv(): rows=[] with file(r'E:\py\py01\Data\system.csv','rb') as f: reads=csv.reader ...

  3. python读取txt、csv、xml

    读取txt,无需引入任何包: user_file = open('user_info.txt','r') lines = user_file.readlines() user_file.close() ...

  4. python之读取和写入csv文件

    写入csv文件源码: #输出数据写入CSV文件 import csv data = [ ("Mike", "male", 24), ("Lee&quo ...

  5. R语言学习——R读取txt、csv、xls和xlsx格式文件

    最近项目中运用到了R读取文件数据,所以把相关好用的.经过验证的方法总结了一下,有效避免下次入坑. 1. R读取txt文件 使用R读取txt文件直接使用read.table()方法进行读取即可,不需要加 ...

  6. python读取txt、csv和excel文件

    一.python读取txt文件:(思路:先打开文件,读取文件,最后用for循环输出内容) fp = open('test.txt','r') lines = fp.readlines() fp.clo ...

  7. Python+Selenium学习笔记15 - 读取txt和csv文件

    读取txt的内容并用百度查找搜索 1 # coding = utf-8 2 3 from selenium import webdriver 4 import time 5 6 # 打开浏览器 7 d ...

  8. 读取txt写入excel

    import csv #实现的思想:首先从txt中读取所有的内容,NUM=1当做键,其他当做值,如果查找缺少a,b,c,d,e,f,g# 则NUM不会添加到字典中,然后通过所有的NUM和字典中的KEY ...

  9. 【Python】sasa版:文件中csv读取在写入csv读取的数据和执行是否成功。

    sasa写的文件(包含解析文字) # coding=utf- from selenium import webdriver from time import sleep import keyword ...

随机推荐

  1. Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password

    Centos7.5 执行ansible命令报错 问题: [root@m01 ~]# ansible servers -a "hostname" -i ./hosts -u root ...

  2. Mybatis的in使用

    foreach元素的属性主要有 item,index,collection,open,separator,close.    item表示集合中每一个元素进行迭代时的别名,    index指 定一个 ...

  3. 获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”

    function getNowFormatDate() {     var date = new Date();     var seperator1 = "-";     var ...

  4. 何为TLC、MLC、SLC?【转】

    本文转载自:https://blog.csdn.net/weixin_38233274/article/details/79310316 1.一块SSD由主控.DRAM缓存和NAND闪存三种芯片所组成 ...

  5. e信与酸酸结合开wifi使用路由器上网

    关于e信"正常情况下"使用路由器网上是有方法的,入户线插上lan,电脑接lan拨号 我想要说的是连接e信后使用路由器上网,并且是绝对正常的思维 手机也是可以连接上wifi,但是手机 ...

  6. IE8下面parseInt('08')、parseInt('09')会转成0

    例子: <html> <body> <script type="text/javascript"> for(var i=1;i<=20;i ...

  7. BZOJ 3707 圈地

    闲扯 BZOJ权限题,没有权限,哭了 然后DBZ不知道怎么回事,\(O(n^3)\)直接压过去了... 备忘 叉积的计算公式\(x_1y_2\) 思路 n^3 暴力枚举显然 n^2 正解的思路有点神, ...

  8. An Empirical Evaluation of Generic Convolutional and Recurrent Networks for Sequence Modeling

    An Empirical Evaluation of Generic Convolutional and Recurrent Networks for Sequence Modeling 2018-0 ...

  9. oogle advertiser api开发概述——速率限制

    速率限制 为了向遍布全球的 AdWords API 用户提供可靠的服务,我们使用令牌桶算法来衡量请求数并确定每秒查询数 (QPS) 速率.这样做的目的是阻止恶意的或不可控的软件大量入侵 AdWords ...

  10. 移动端开发:使用jQuery Mobile还是Zepto

    原:http://blog.csdn.net/liubinwyzbt/article/details/51446771 jQuery Mobile和Zepto是移动端的js库.jQuery Mobil ...