当前位置:网站首页>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)
边栏推荐
- 【高并发】通过源码深度分析线程池中Worker线程的执行流程
- NowCoderTOP7-11——持续更新ing
- C3d model pytorch source code sentence by sentence analysis (I)
- 软件测试技术之跨平台的移动端UI自动化测试(上)
- 学习周刊 - 总第 63 期 - 一款开源的本地代码片段管理工具
- Basic experiment of microwave technology - Filter Design
- BGP联邦实验
- The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
- 史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
- 代码的表示学习:CodeBERT及其他相关模型介绍
猜你喜欢
随机推荐
NowCoderTOP12-16——持续更新ing
Flask框架——Flask-WTF表单:数据验证、CSRF保护
How to optimize the performance when the interface traffic increases suddenly?
AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic
mysql高级语句(一)(总有一个人的出现,让你的生活不再继续糟糕)
feign客户端请求之LoadBalancerLifecycle生命周期
微信小程序版本更新如何通知用户?
HCIA experiment (09)
NowCoderTOP7-11——持续更新ing
HDD Hangzhou station full experience
SQL语言(二)
【域泛化】2022 IJCAI领域泛化教程报告
HCIP(11)
Hcip experiment (01)
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!
【高并发】如何实现亿级流量下的分布式限流?这些理论你必须掌握!!
【高并发】通过源码深度分析线程池中Worker线程的执行流程
[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)
Signal integrity (SI) power integrity (PI) learning notes (XXXIII) 102 general design rules to minimize signal integrity problems
Learn NLP with Transformer (Chapter 7)








