当前位置:网站首页>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)
边栏推荐
- The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
- Signal integrity (SI) power integrity (PI) learning notes (XXXIV) 100 rules of thumb for estimating signal integrity effects
- Flask framework - flask WTF form: data validation, CSRF protection
- AI technology stack is too huge! Wu Enda gives career planning: lifelong learning
- Flask框架——Flask-WTF表单:数据验证、CSRF保护
- BGP federal experiment
- [domain generalization] 2022 IJCAI domain generalization tutorial Report
- [servlet] request parsing
- Dataset and dataloader data loading
- JS hash table 01
猜你喜欢

Reinforcement Learning 强化学习(四)

AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic

【flask高级】结合源码详解flask的运行机制(出入栈)

Flask framework - flask WTF form: data validation, CSRF protection
Learn NLP with Transformer (Chapter 2)

学习路之PHP--Phpstudy 提示 Mysqld.Exe: Error While Setting Value ‘NO_ENGINE_SUBSTITUTION 错误的解决办法

HCIP(11)

BeautifulSoup的一些用法

从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计

Flask framework - Message flash
随机推荐
Understand the life cycle and route jump of small programs
Shell 脚本参数传递时有 \r 换行符问题
MySQL master-slave replication and read-write separation
Tree dynamic programming
Learn NLP with Transformer (Chapter 4)
Software Testing Technology: cross platform mobile UI automated testing (Part 1)
A troubleshooting record of DirectShow playback problems
数字孪生万物可视 | 联接现实世界与数字空间
【高并发】通过源码深度分析线程池中Worker线程的执行流程
Electromagnetic field and electromagnetic wave experiment I familiar with the application of MATLAB software in the field of electromagnetic field
100W!
[flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
上周热点回顾(7.18-7.24)
Learn NLP with Transformer (Chapter 6)
2021 CEC written examination summary
30000 word express Servlet
DICOM medical image viewing and browsing function based on cornerstone.js
信号完整性(SI)电源完整性(PI)学习笔记(三十四)100条估计信号完整性效应的经验法则
Esp8266 uses drv8833 drive board to drive N20 motor
What is MySQL transaction