标签:建模 mode 文件读取 proc 循环 lang 出现 ffffff 完成
1.三元运算符三元运算符又称三目运算,是对简单的条件语句的简写
语法:val =1 if 条件成立 else 2
简单条件语句:
a = 0
if a == 0:
val = 0
else:
val = 1
print(val)
三目运算:
val = 0 if a == 0 else 2
print(val)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
0
Process finished with exit code 0
错误案例
a = 0
val = 0 if a == 0 else val = 2
print(val)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
File "E:/PythonProject/python-test/BasicGrammer/test.py", line 8
val = 0 if a == 0 else val = 2
^
SyntaxError: can‘t assign to conditional expression
Process finished with exit code 1
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
f = open(file="读文件.txt", mode="r", encoding="utf-8")
data = f.read()
f.close()
print(type(data))
print(data)
# file="读文件.txt" 表示文件路径
# mode="r" 表示"只读"(可以修改为其他)
# encoding="utf-8" 表示将硬盘上的0101010101按照utf-8的规则去"断句",再将"断句"后的每一段01010101转换成unicode的0101010,unicode对照表中有010101和字符的对应关系
# f.read()表示读取多有内容,内容是已经转换完成的"字符串"
# f.close() 表示关闭文件
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
<class ‘str‘>
哈哈
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
f = open(file="读文件.txt", mode="rb")
data = f.read()
f.close()
print(type(data))
print(data)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
<class ‘bytes‘>
b‘\xe5\x93\x88\xe5\x93\x88\r\n‘
Process finished with exit code 0
# mode="rb" 表示只读(可以修改为其他)
# 数据类型是"bytes"
# rb 与 r 的区别:
# "rb"在打开文件的时候不需要指定encoding,因为直接以rb模式打开文件
# rb是二进制模式,数据读到内存中直接就是bytes模式,如果想要看内容,还需要手动decode
# 因此文件打开阶段不要指定编码
# 手动decode也需要知道源文件的编码,否则,decode后还是乱码的
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import chardet
result = chardet.detect(open(file="读文件.txt", mode="rb").read())
print(result)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
{‘encoding‘: ‘utf-8‘, ‘confidence‘: 0.7525, ‘language‘: ‘‘}
Process finished with exit code 0
# 查看到源文件编码是utf-8,下面就使用utf-8解码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
data = open(file="读文件.txt", mode="rb").read()
print(data.decode(encoding="utf-8"))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
哈哈
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
f = open(file="读文件.txt", mode="r", encoding=‘utf-8‘)
print("--------readline-------")
print(f.readline())
print(f.readline())
print(type(f.readline()))
# readline读取一行,这一行的数据类型是str
print("--------readline-------")
print("--------readlines-------")
print(f.readlines())
print(type(f.readlines()))
# readlines是全部读取出来,数据类型是list
print("--------readlines-------")
print("--------read-------")
print(f.read())
print(type(f.read()))
# read是全部读取出来,数据类型是str
# 这里为空,是因为上面的代码已经把文件读取完了
print("--------read-------")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
--------readline-------
马纤羽 北京 173 50 13744234523
乔亦菲 广州 172 52 15823423525
<class ‘str‘>
--------readline-------
--------readlines-------
[‘刘诺涵 \t北京\t170\t48\t18623423765\n‘, ‘岳妮妮 \t北京\t177\t54\t18835324553\n‘, ‘贺婉萱 \t北京\t174\t52\t18933434452\n‘, ‘叶梓萱\t上海\t171\t49\t18042432324\n‘, ‘杜姗姗 北京 167 49 13324523342\n‘, ‘black girl 河北 167 50 13542342233‘]
<class ‘list‘>
--------readlines-------
--------read-------
<class ‘str‘>
--------read-------
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
f = open(file="读文件.txt", mode="r", encoding=‘utf-8‘)
for line in f:
print(line)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
马纤羽 北京 173 50 13744234523
乔亦菲 广州 172 52 15823423525
罗梦竹 北京 175 49 18623423421
刘诺涵 北京 170 48 18623423765
岳妮妮 北京 177 54 18835324553
贺婉萱 北京 174 52 18933434452
叶梓萱 上海 171 49 18042432324
杜姗姗 北京 167 49 13324523342
black girl 河北 167 50 13542342233
Process finished with exit code 0
# read()方法读取文件,是一下子把整个文件都读取到内存中,一次性读完,但是如果文件很大,会出现内存溢出的情况,所以可以每次读取部分数据
# 为什么每行的后面都有一个空行呢?
# 因为print()函数就是自动会换行,而且每行的后面已经存在\n了,所以会有个空行
# 上面使用readlins()函数时,可以看到每行结尾都是有\n的
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
f = open(file="写文件.txt", mode="w", encoding=‘utf-8‘)
# "这时文件还没真正写入硬盘中,还在内存中,可以f.write()后调用f.flush()把内容写入到硬盘中,防止数据丢失"
f.write(‘写入文件‘)
# "这时文件才写入硬盘"
f.close()
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Process finished with exit code 0
查看文件内容
写入文件
# mode = "w" 表示只写,"w"是创建模式,会清空之前的内容,重新写入,比较危险
# encoding=‘utf-8‘ 将要写入的unicode字符串编码成utf-8格式
# f.write() 表示写入内容,写入的内容是unicode字符串类型,内部会根据encoding转换成特定编码的01010101,即字节类型
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
f = open(file="写文件.txt", mode="wb")
f.write(‘写入文件‘.encode("utf-8"))
f.close()
# "写入文件的时候,需要手动把写入的内容encode(),指定编码"
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Process finished with exit code 0
查看文件内容
写入文件
标签:建模 mode 文件读取 proc 循环 lang 出现 ffffff 完成
原文地址:https://blog.51cto.com/10983441/2387195