当前位置:网站首页>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)
边栏推荐
- 2021 scenery written examination summary
- The integration of two in one has a long way to go
- 史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
- Ue4.26 source code version black screen problem of client operation when learning Wan independent server
- MLX90640 红外热成像仪测温模块开发笔记(五)
- Learn NLP with Transformer (Chapter 7)
- Learn NLP with Transformer (Chapter 2)
- HCIP(13)
- 【云享新鲜】社区周刊·Vol.72- 2022华为开发者大赛中国区首场开幕式启动;华为云KooMessage火热公测中…
- 学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
猜你喜欢

Flask框架——Session与Cookie

100W!

2021 CEC written examination summary

What is MySQL transaction

最详细的mysql索引解析(文末附赠思维导图)

HCIP(13)
![[Blue Bridge Cup training 100 questions] scratch Taiji diagram Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question No. 22](/img/d5/56173050f62d5b6fa336ff8d257fca.png)
[Blue Bridge Cup training 100 questions] scratch Taiji diagram Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question No. 22

JS hash table 01

HCIA实验(09)

MLX90640 红外热成像仪测温模块开发笔记(五)
随机推荐
企业实践开源的动机
Electromagnetic field and electromagnetic wave experiment I familiar with the application of MATLAB software in the field of electromagnetic field
DICOM medical image viewing and browsing function based on cornerstone.js
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!
Ue4.26 source code version black screen problem of client operation when learning Wan independent server
Learn NLP with Transformer (Chapter 5)
HCIP(11)
The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
Shell 脚本参数传递时有 \r 换行符问题
HCIA实验(09)
SQL语言(一)
Flask framework -- flask caching
AI系统前沿动态第43期:OneFlow v0.8.0正式发布;GPU发现人脑连接;AI博士生在线众筹研究主题
Digital twin everything can be seen | connecting the real world and digital space
软件测试技术之跨平台的移动端UI自动化测试(上)
Flask framework - Message flash
NowCoderTOP7-11——持续更新ing
30000 word express Servlet
HCIA实验(06)
Basic experiment of microwave technology - Filter Design