def
writerCsv2
()
:
f = open(
"data.csv"
,
"w"
)
writer = csv.DictWriter(f, [
"name"
,
"age"
])
for
i
in
range(
100
):
dct = {
"name"
:i+
1
,
"age"
: i+
2
}
writer.writerow(dct)
f.close()
print(
"写入成功!"
)
writerCsv2()
要读取的文件
# MontyPythonAlbums.csv
Name,Year
Monty Python's Flying Circus,1970
Another Monty Python Record,1971
Monty Python's Previous Record,1972
# 按行读取列表
def readerCsv1():
# 读取网络文件
# url = "http://www.pythonscraping.com/files/MontyPythonAlbums.csv"
# data = urlopen(url).read()
# 读取本地文件
data = open("MontyPythonAlbums.csv", "r").read().decode('utf-8')
print type(data)
data_file = StringIO(data) # 字符串转为io对象
csv_reader = csv.reader(data_file)
for row in csv_reader:
print(row)
['Name', 'Year']
["Monty Python's Flying Circus", '1970']
# readerCsv1()
# 按行读取列表
def readerCsv2():
f = open("MontyPythonAlbums.csv", "r")
csv_reader = csv.reader(f)
for row in csv_reader:
print(row)
['Name', 'Year']
["Monty Python's Flying Circus", '1970']
f.close()
# readerCsv2()
# 按行读取字典,第一行为key
def readerCsv3():
f = open("MontyPythonAlbums.csv", "r")
csv_reader = csv.DictReader(f)
for row in csv_reader:
print(row)
# {'Name': "Monty Python's Flying Circus", 'Year': '1970'}
f.close()
# readerCsv3()
python矩阵怎么赋值 python怎么进行矩阵运算
python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包。1.numpy的导入和使用from numpy import *;#导入numpy的库函数
import numpy as np; #这个方式使用numpy的函数时,需要以np.开头。2.矩阵的创建由一维或二维数据创建矩阵>>> from numpy import *
java 检测 cmyk java 检测内存
概述:内存检测Java实现的内存检测代码,可检测出总内存空间大小、剩余内存空间大小、已使用内存多少,若看懂了代码,自己扩展一下还是不错的。import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Memory extends MIDlet implements CommandListene