从文件中读取数据
读取整个文件
f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\0.txt'
with open(f_path) as f:
contents=f.read()
print(contents)
函数open返回一个表示文件的对象,对象存储在f中
关键字with在不需要访问文件时将其自动关闭。
读取出的内容以字符串形式保存在contents里
逐行读取
f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\0.txt'
with open(f_path) as f:
for line in f:
print(line)
发现行间距比读取整个文件时大了很多,因为每一行都有末尾都有一个看不见的换行符,而print语句也会加上换行符。要消除这些多余空白行,可以在print语句中使用rstrip()
with open(f_path) as f:
for line in f:
print(line.rstrip())
创建一个包含文件各行内容的列表
使用系统关键字with时,open()返回的文本对象只在with代码块内使用。如果要在with代码块之外访问文件的内容,可以在with代码块中将文件各行存储在一个列表中,并在with代码块外使用该列表。
with open(f_path) as f:
lines = f.readlines()
for line in lines:
print(line.rstrip())
读取文本文件时,python将其中的所有文本都解读为字符串。如果读取的是数字,并要将其作为数值使用,就必须使用函数int()将其转化为整数。或使用float()将其转换为浮点数。
运行python文件报SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: tr
1、在路径前面加r,即保持字符原始值的意思。
sys.path.append(r'c:\Users\mshacxiang\VScode_project\web_ddt')
2、替换为双反斜杠
sys.path.append('c:\\Users\\mshacxiang\\VScode_project\\web_ddt')
3、替换为正斜杠
sys.path.append('c:/Users/mshacxiang/VScode_project/web_ddt')
写入文件
写入空文件
f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\my.txt'
with open(f_path, 'w') as f:
f.write('I love programming!')
实参‘w’告诉python以写入模式打开文件。打开文件时,可以指定读取方式(‘r’:读取 ‘w’:写入 ‘a’ 附加 ‘r+’ 读取和写入 )默认为只读模式。
若写入的文件不存在,函数open()将自动创建。以写入模式打开文件时,注意如果文件已存在,python将在返回文件对象前清空文件。若不想清空,可以以追加的方式创建。
python只将字符串写入文本文件。要将数值数据存储到文本文件中,必须先使用函数str()将其转换为字符串格式。读出的也是字符串
写入多行
函数write不会在写入的文本末尾添加换行符
f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\my.txt'
with open(f_path, 'w') as f:
f.write('I love programming!')
f.write('I love English too!')
f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\my.txt'
with open(f_path, 'w') as f:
f.write('I love programming!\n')
f.write('I love English too!\n')
'''生成数组'''
x_vals = np.linspace(0, 10, 5)
print(x_vals)
'''转化数组为矩阵'''
x_vals_column = np.transpose(np.matrix(x_vals))
print(x_vals_column)
'''生成一个列矩阵如下:'''
ones_column = np.transpose(np.matrix(np.repeat(1, 5)))
print(ones_column)
'''将2个矩阵按列合并'''
A = np.column_stack((x_vals_column, ones_column))
print(A)
'''将2个矩阵按行合并'''
B = np.row_stack((x_vals_column, ones_column))
print(B)
如果你要取前几行
a=c[0:3,:] 前0,1,2行
b = c[0,2:4] 第0行的第2和第3列不包括第4列,因为列下标也是从0开始的
d = c[2:4,2:4] 取中间的2-4行的2-4列
e = c[0,:] 取第0行所有数据
f = c[:,1] 取第1列所有数据
g = c[::2,::2] 两个冒号后面表示步长为2
读取txt文件,对文件进行修改以后重新写入到txt文件
import numpy as np
f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\0.txt'
with open(f_path) as f:
contents=f.read()
print(contents)
print(type(contents))
a = contents.split()
print(a)
a = np.array(a)
a = a.reshape(5,8)
a = a[:,1:7]
print(a[0,0])
print(type(a ))
f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\my.txt'
with open(f_path, 'w') as f:
'''按列生成矩阵'''
'''np.savetxt(f, np.column_stack(a), fmt='%s')'''
'''按行生成矩阵'''
np.savetxt(f, np.row_stack(a), fmt='%s')