当前位置:网站首页>txt转csv文件,隔行出现空行
txt转csv文件,隔行出现空行
2022-07-25 10:27:00 【fatfatmomo】
在用csv.writer写入文件的时候发现中间有多余的空行。
最早打开方式只是‘w’,会出现多余的空行,网上建议使用binary形式‘wb’打开可以解决问题:python正常写入文件的时候,每行的结束默认添加'n’,即0x0D,而writerow命令的结束会再增加一个0x0D0A,因此对于windows系统来说,就是两行,而采用’ b'参数,用二进制进行文件写入,系统默认是不添加0x0D的。
不过只能在python2下运行,python3报错:TypeError: a bytes-like object is required, not 'str'
该问题解决方法:在open()内增加一个参数newline='' 即可
with open('data/Gowalla_edges.csv', 'w',newline='') as csvfile:
spamwriter = csv.writer(csvfile, dialect='excel')
# 读要转换的txt文件,文件每行各词间以@@@字符分隔
with open('data/Gowalla_edges.txt', 'r') as filein:
for line in filein:
print(line)
line_list = line.strip('\n').split('\t')
spamwriter.writerow(line_list)
边栏推荐
- Gan, why '𠮷 𠮷'.Length== 3 ??
- mysql高级语句(一)(总有一个人的出现,让你的生活不再继续糟糕)
- ESP8266 使用 DRV8833驱动板驱动N20电机
- Learn NLP with Transformer (Chapter 3)
- 2021 qunar written examination summary
- BGP federal experiment
- HCIA experiment (10) nat
- [domain generalization] 2022 IJCAI domain generalization tutorial Report
- Guys, flick CDC table API, Mysql to MySQL, an application that can
- 【flask高级】结合源码详解flask的运行机制(出入栈)
猜你喜欢

HCIP(11)

Visual thematic map of American airport go style: ArcGIS Pro version

PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying

HCIP (01)
学习周刊 - 总第 63 期 - 一款开源的本地代码片段管理工具

redis 哨兵,高可用的执行者

100W了!
Learn NLP with Transformer (Chapter 4)

史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)

Flask框架——Session与Cookie
随机推荐
Learn NLP with Transformer (Chapter 4)
Flask框架——Flask-WTF表单:文件上传、验证码
Flask framework - flask WTF form: data validation, CSRF protection
TPS calculation in performance test [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
[flask advanced] deeply understand the application context and request context of flask from the source code
100W了!
BGP federal experiment
NowCoderTOP12-16——持续更新ing
最详细的mysql索引解析(文末附赠思维导图)
The idea has been perfectly verified again! The interest rate hike is approaching, and the trend is clear. Take advantage of this wave of market!
HCIA experiment (06)
Last week's hot review (7.18-7.24)
Leetcode 560 prefix and + hash table
Learn NLP with Transformer (Chapter 6)
Reinforcement learning (III)
Hcip experiment (02)
Digital twin everything can be seen | connecting the real world and digital space
【高并发】通过源码深度分析线程池中Worker线程的执行流程
HCIP(11)
SQL语言(二)