Python如何构建xml文档

Jackey Python 1,534 次浏览 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.text = '8.80' e2.appen...

Python如何解析简单的xml文档

Jackey Python 1,494 次浏览 0 评论 ,
xml示例 <?xml version="1.0" ?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</g...

Python如何读写json数据

Jackey Python 1,678 次浏览 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)) # 压缩字符串长度,去掉空格 print(json.dumps(d, ...

Python如何读写csv数据

Jackey Python 1,645 次浏览 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', 'w', encoding='gbk...

Python如何使用临时文件

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

Python如何访问文件的状态

Jackey Python 1,633 次浏览 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(s.st_mode) impor...

Python如何读写文本文件

Jackey Python 1,590 次浏览 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('你好,ijackey.com') f.cl...

Python如何去掉字符串中不需要的字符

Jackey Python 1,624 次浏览 0 评论 ,
# 如何去掉字符串中不需要的字符 s = ' abc 123 ' print(s.strip()) print(s.lstrip()) print(s.rstrip()) s = '----abc+++++' print(s.strip('-+')) s = 'abc:123' print(s[:3] + s[4:]) s = '\tabc\t123\txyz' ...

Python如何对字符串进行左、中、右对齐

Jackey Python 1,675 次浏览 0 评论 ,
# 如何对字符串进行左、中、右对齐 s = 'abc' print(s.ljust(20) + '|') print(s.ljust(20), '=') print(s.rjust(20)) print(s.center(20) + '|') # < 描述左对齐 # > 描述右对齐 # ^ 描述居中对齐 print(format(s, ...

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

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

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

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

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

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