当前位置:网站首页>Txt to CSV file, blank lines appear every other line
Txt to CSV file, blank lines appear every other line
2022-07-25 11:25:00 【fatfatmomo】
In use csv.writer When writing a file, I found an extra blank line in the middle .
The earliest way to open it is ‘w’, There will be extra blank lines , It is suggested to use binary form ‘wb’ Open to solve the problem :python When writing files normally , The end of each line is added by default 'n’, namely 0x0D, and writerow The end of the command will add another 0x0D0A, So for windows system , It's just two lines , And use ’ b' Parameters , Write files in binary , By default, the system does not add 0x0D Of .
But only in python2 Run under ,python3 Report errors :TypeError: a bytes-like object is required, not 'str'
The solution to this problem : stay open() Add a parameter in newline='' that will do
with open('data/Gowalla_edges.csv', 'w',newline='') as csvfile:
spamwriter = csv.writer(csvfile, dialect='excel')
# Read to convert txt file , Each line of the document is marked with @@@ Character separation
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)
边栏推荐
- My colleague looked at my code and exclaimed: how can I use a singleton in unity
- 最详细的mysql索引解析(文末附赠思维导图)
- Reinforcement Learning 强化学习(三)
- tensorflow 调用多块GPU的一些错误
- Learn NLP with Transformer (Chapter 2)
- 从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计
- Shell - Chapter 8 exercise
- [树] 100. 相同的树
- The integration of two in one has a long way to go
- Learn NLP with Transformer (Chapter 8)
猜你喜欢

企业实践开源的动机

SQL语言(六)
Learn NLP with Transformer (Chapter 4)

NowCoderTOP1-6——持续更新ing

学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”

HCIA experiment (09)

Esp32c3 based on the example tutorial of esp32 Rainmaker development under Arduino framework
Learn NLP with Transformer (Chapter 3)

C# Newtonsoft. Jason advanced usage
![[flask advanced] deeply understand the application context and request context of flask from the source code](/img/67/88c21967da28d09694568ebdaae44c.png)
[flask advanced] deeply understand the application context and request context of flask from the source code
随机推荐
Let sports happen naturally, and fire creates a new lifestyle
ESP8266 使用 DRV8833驱动板驱动N20电机
Esp8266 uses drv8833 drive board to drive N20 motor
HCIA experiment (06)
Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
Learn Luzhi PHP -- tp5.0 uses Chinese as an alias and reports "unsupported data expression"
mysql事务是什么
MLX90640 红外热成像仪测温模块开发笔记(五)
Detailed explanation of the implementation method of DNS separation and resolution
[high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
机智云物联网平台 STM32 ESP8266-01S 简单无线控灯
Learn NLP with Transformer (Chapter 4)
HCIA experiment (07) comprehensive experiment
Code representation learning: introduction to codebert and other related models
爬虫基础一
上周热点回顾(7.18-7.24)
Learn NLP with Transformer (Chapter 8)
Shell 脚本参数传递时有 \r 换行符问题
shell-第五章作业
Mlx90640 infrared thermal imager temperature measurement module development notes (V)