merge lines

根据行的特征进行合并

1
2
3
4
5
6
data = open('file.txt').read.split('\n')
for i, line in enumerate(data):
if line.startswith(":"):
data[i-1] = data[i-1] + line
data.pop[i]
print('\n'.join(data)

根据行数转化相应的列

1
2
3
4
5
6
7
input:
1
2
3
4
5
6

转换为

1
2
3
output
1,2,3
4,5,6

需要利用控制行来换行

1
2
3
4
5
6
7
8
9
10
11
data_list = []
with open(filepath, 'r') as f:
file = f.read()
data_list.append(file)

## 设置步长为3
three_lines = range(0, len(data_list), 3)
with open('output.txt', 'w') as f:
for num, line in enumerate(data_list):
if num in three_lines:
f.write(','.join(data_list[num:num+3])+'\n')

计算文件行数

1
2
3
## 直接计算文件行数
lines = len(open('/Users/macbook/Downloads/ascii_dora2.txt') .readline())
print(lines)
  • 使用readlines时候有的时候会比readline少一行,原因在于readlines会将文件拆分为一个列表,遇到最后一行如果没有换行符则只统计到前面那一行。而readline每次都读取一行,因此会造成一些差别。
  • 再次说一下read,将文件放入一个字符串变量进行后续操作,对于大文件的话需要慎重使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
## 初始化count2
count2 = -1
for count2, line in enumerate(open('/Users/macbook/Downloads/ascii_dora2.txt')):
pass
count2 += 1
print(count2)

## method 2
### 统计有多少换行符,同readlines
count3 = 0
file1 = open('/Users/macbook/Downloads/ascii_dora2.txt', 'r')
while True:
buffer = file1.read(8192*1024)
if not buffer:
break
count3 += buffer.count('\n')
file1.close()
print(count3)

缓存行

1
2
3
4
5
import linecache
## 读取指定行
count = linecache.getline(file, linenum)
## 读取文件内容
str = linecache.getlines(file)