Python如何构建xml文档

Jackey Python 1,540 次浏览 0 评论 ,
# 如何构建xml文档 from xml.etree.ElementTree import Element, ElementTree, tostring e = Element('Data') e.set('name', 'abc') e.text = '123' e2 = Element('Row') e3 = Element('Open') e3.tex...

Python如何读写json数据

Jackey Python 1,683 次浏览 0 评论 ,
# 如何读写json数据 import json l = [1, 2, 'abc', {'name': 'Bob', 'age': 13}] print(json.dumps(l)) d = {'b': None, 'a': 5, 'c': 'abc'} print(json.dumps(d)) # 压缩字符串长度,去掉空格 ...

Python如何读写csv数据

Jackey Python 1,650 次浏览 0 评论 ,
# 如何读写csv数据 import csv # 读 rf = open('test.csv', 'r', encoding='gbk') reader = csv.reader(rf) print(next(reader)) # for row in reader: print(row) # 写 wf = open('test_copy.csv'...

Python如何使用临时文件

Jackey Python 1,610 次浏览 0 评论 ,
# 如何使用临时文件 from tempfile import TemporaryFile, NamedTemporaryFile # 系统中无法找到这个临时文件,只能本进程访问 f = TemporaryFile() f.write(b'abcdef' * 100000) f.seek(0) print(f.re...

Python如何访问文件的状态

Jackey Python 1,638 次浏览 0 评论 ,
# 如何访问文件的状态 import os print(os.stat('p.txt')) print(os.lstat('p.txt')) f = open('p.txt') print(os.fstat(f.fileno())) s = os.stat('p.txt') print(s) # 文件类型,标志位构成 print...

Python如何读写文本文件

Jackey Python 1,596 次浏览 0 评论 ,
# 如何读写文本文件 s = '你好' print(s.encode('utf8')) print(s.encode('gbk')) # b 代表byte print(b'abc') # wt t 是默认方式 f = open('py3.txt', 'wt', encoding='utf8') f.write('你好,i...

Python如何调整字符串中文本的格式

Jackey Python 1,586 次浏览 0 评论 ,
# 如何调整字符串中文本的格式 log = open('../test.log').read() import re # 将日期2021-04-23 替换成 04/23/2021 格式 # r代表原始字符串,转义字符在这里作为普通字符串使用 # res = re.sub('(\d{4}...

Python如何拆分含有多种分隔符的字符串

Jackey Python 1,651 次浏览 0 评论 ,
# 如何拆分含有多种分隔符的字符串 import os # 单一分隔符 x = os.popen('ps aux').read().splitlines() s = x[-1] # s.split() # 如果是空格、制表符等空白分隔,可以不用传参 print(s.split()) ...

Python如何对迭代器做切片操作

Jackey Python 1,328 次浏览 0 评论 ,
# 如何对迭代器做切片操作 f = open('test.log') # f.readlines() 一次性把文件读入到内存当中 # lines = list(f.readlines()) # print(lines[100:300]) # for line in f: # print(line) from i...
Go