Python之文件读取
1. txt文件的读写
1.1 每行数据不是纯文字(除表头外)
- 测试用的文本文件名称为:
test1.txt
,文件绝对路径为D:\test1.txt
。文件内容如下:
id,name,sex,age,salary
1,tom,male,21,25000.0
2,alice,female,18,12000.0
def test01():
filename = r"D:\test1.txt"
filename = "D:\\test1.txt"
filename = "./test1.txt"
filename = "test1.txt"
with open(filename, 'r') as f:
for line in f:
print(line)
'''
id,name,sex,age,salary
1,tom,male,21,25000.0
2,alice,female,18,12000.0
'''
print("---------------------------------------")
with open(filename, 'r') as f:
data = f.read()
print(data)
'''
id,name,sex,age,salary
1,tom,male,21,25000.0
2,alice,female,18,12000.0
'''
print("---------------------------------------")
with open(filename, 'r') as f:
data = f.readlines()
for line in data:
line = line.strip("\n")
print(line)
'''
id,name,sex,age,salary
1,tom,male,21,25000.0
2,alice,female,18,12000.0
'''
print("---------------------------------------")
dict = {
}
with open(filename, 'r') as f:
for line in f.readlines()[1:]:
item = line.strip("\n").split(',')
id, name, sex, age, salary = item[0], item[1], item[2], int(item[3]), float(item[4])
dict[id] = [name, sex, age, salary]
print(dict)
'''
{'1': ['tom', 'male', 21, 25000.0], '2': ['alice', 'female', 18, 12000.0]}
'''
filename = "test2.txt"
con = ["hello\n", "12.0\n"]
with open(filename, 'w') as f:
f.writelines(con)
print("---------------------------------------")
with open(filename, 'a') as f:
f.write("\n")
for k, v in dict.items():
item = str(k) + "," + ",".join([str(x) for x in v]) + "\n"
f.write(item)
'''
hello
12.0
1,tom,male,21,25000.0
2,alice,female,18,12000.0
'''
1.2 每行数据是纯文字(除表头外)
- 测试用的文本文件名称为:
test3.txt
,文件绝对路径为D:\test3.txt
。文件内容如下:
Sepal.Length,Sepal.Width,Petal.Length,Petal.Width
5.1,3.5,1.4,0.3
4.9,3,1.4,0.2
4.7,3.2,1.3,0.2
def test02():
filename = "test3.txt"
data = []
with open(filename, 'r') as f:
for line in f.readlines()[1:]:
line = [float(x) for x in line.strip("\n").split(",")]
data.append(line)
print(data)
import numpy as np
a = np.array(data)
print(a)
2. csv文件的读写
def test03():
filename = "test4.csv"
data = []
with open(filename, 'r') as f:
for line in f.readlines()[1:]:
data.append(line.strip("\n").split(","))
print(data)
print("---------------------------------------")
import csv
with open(filename, 'r') as f:
reader = csv.reader(f)
data = [row for row in reader]
data = data[1:]
print(data)
print("---------------------------------------")
import pandas as pd
import numpy as np
df = pd.read_csv(filename)
print(df)
'''
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 7.0 3.2 4.7 1.4 versicolor
3 6.4 3.2 4.5 1.5 versicolor
4 6.3 3.3 6.0 2.5 virginica
5 5.8 2.7 5.1 1.9 virginica
'''
a = df.values
print(a)
'''
[[5.1 3.5 1.4 0.2 'setosa']
[4.9 3.0 1.4 0.2 'setosa']
[7.0 3.2 4.7 1.4 'versicolor']
[6.4 3.2 4.5 1.5 'versicolor']
[6.3 3.3 6.0 2.5 'virginica']
[5.8 2.7 5.1 1.9 'virginica']]
'''
print(a.dtype)
print(type(a[0][0]))
print(type(a[0][4]))
data = np.array(a[:, :-1], dtype=float)
print(data)
'''
[[5.1 3.5 1.4 0.2]
[4.9 3.0 1.4 0.2]
[7.0 3.2 4.7 1.4]
[6.4 3.2 4.5 1.5]
[6.3 3.3 6.0 2.5]
[5.8 2.7 5.1 1.9]]
'''
labels = a[:, -1].tolist()
print(type(labels))
print(labels)
'''
['setosa' 'setosa' 'versicolor' 'versicolor' 'virginica' 'virginica']
'''
print("---------------------------------------")
import csv
headers = ['class', 'name', 'sex', 'height', 'year']
rows = [
[1, 'xiaoming', 'male', 168, 23],
[1, 'xiaohong', 'female', 162, 22],
[2, 'xiaozhang', 'female', 163, 21],
[2, 'xiaoli', 'male', 158, 21]
]
with open('test5.csv', 'w', newline="") as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(rows)
3. excel文件的读写
def test04():
import pandas as pd
filename = "test6.xlsx"
df = pd.read_excel(filename, engine='openpyxl')
print(df)
'''
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 7.0 3.2 4.7 1.4 versicolor
3 6.4 3.2 4.5 1.5 versicolor
4 6.3 3.3 6.0 2.5 virginica
5 5.8 2.7 5.1 1.9 virginica
'''
print("---------------------------------------")
import xlsxwriter
filename = "test7.xlsx"
headers = ['class', 'name', 'sex', 'height', 'year']
rows = [
[1, 'xiaoming', 'male', 168, 23],
[1, 'xiaohong', 'female', 162, 22],
[2, 'xiaozhang', 'female', 163, 21],
[2, 'xiaoli', 'male', 158, 21]
]
data = [headers] + rows
print(data)
workbook = xlsxwriter.Workbook(filename, {
'nan_inf_to_errors': True})
worksheet = workbook.add_worksheet()
for i in range(len(data)):
worksheet.write_row("A{}".format(i + 1), data[i])
workbook.close()