Python 文件的读写和 json 、csv 的用法

读文件的三种方法:

In [12]: !nl b.txt
     1  今天天气不错
     2  挺风和日丽的

# read() 一次性读取整个文件生成一个字符串
In [13]: with open('b.txt') as f:
    ...:     data = f.read()
    ...:   
In [14]: data
Out[14]: '今天天气不错\n挺风和日丽的\n'

# readline() 读取文件的一行生成字符串
In [15]: with open('b.txt') as f:
    ...:     data1 = f.readline()
    ...:     data2 = f.readline()
    ...:     
In [16]: data1
Out[16]: '今天天气不错\n'
In [17]: data2
Out[17]: '挺风和日丽的\n'

# readlines() 一次性读取全部文件生成列表
# 原文件中每行都是列表的一个元素,每个元素都是字符串
In [18]: with open('b.txt') as f:
    ...:     data = f.readlines()
    ...:     
In [19]: data
Out[19]: ['今天天气不错\n', '挺风和日丽的\n']

JSON 的用法:

# 将数据写入文件,dumps 将数据变成字符串后写入
data = {'one': 'Kobe', 'two': 'James', 'three': 'Jordan'}
with open('filename', 'w') as f:
    f.write(json.dumps(data))

# 或者直接用 dump 方法
with open('filename', 'w') as f:
    json.dump(data, f)

# 读取文件,load 将文件数据(就是字符串)变成字典或列表
with open('filename') as f:
    data = json.load(f)

# 读取数据,loads 将字符串数据转换成字典或列表
In [84]: l = '["Kobe", "James", "Irving", "Nash"]'  # 注意引号的使用,单引号在外
In [85]: data = json.loads(l)
In [86]: data
Out[86]: ['Kobe', 'James', 'Irving', 'Nash']

# 读取文件并写入另一个文件
In [98]: !nl helloshiyanlou.json
     1  {
     2      "title": "Hello",
     3      "created_time": "12:00:00",
     4      "content": "Welcome to shiyanlou"
     5  }
In [99]: with open('helloshiyanlou.json') as f:
    ...:     with open('haha.json', 'w') as g:
    ...:         g.write(json.dumps(json.load(f)))
    ...:         
In [100]: !nl haha.json
     1  {"content": "Welcome to shiyanlou", "created_time": "12:00:00", "title": "Hello"}

CSV 的用法:

# 读取文件,用 reader 读取的结果是个迭代器,文件关闭后此迭代器无法读取数据
In [125]: !nl s.csv
     1  Symbol,Price,Date,Time,Change,Volume
     2  "AA",39.48,"6/11/2007","9:36am",-0.18,181800
     3  "AIG",71.38,"6/11/2007","9:36am",-0.15,195500
     4  "AXP",62.58,"6/11/2007","9:36am",-0.46,935000
     5  "BA",98.31,"6/11/2007","9:36am",+0.12,104800
     6  "C",53.08,"6/11/2007","9:36am",-0.25,360900
     7  "CAT",78.29,"6/11/2007","9:36am",-0.23,225400
In [126]: with open('s.csv') as f:
     ...:     data = csv.reader(f)
     ...:     
In [127]: isinstance(data, Iterator)
Out[127]: True
In [128]: next(data)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in ()
----> 1 next(data)
ValueError: I/O operation on closed file.

# 可以把读取到的数据转换成 tuple 或 list
# tuple 或 list 里每个元素都是列表,每个子列表里的元素都是字符串
In [129]: with open('s.csv') as f:
     ...:     data = list(csv.reader(f))
     ...:     
In [130]: data
Out[130]: 
[['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume'],
 ['AA', '39.48', '6/11/2007', '9:36am', '-0.18', '181800'],
 ['AIG', '71.38', '6/11/2007', '9:36am', '-0.15', '195500'],
 ['AXP', '62.58', '6/11/2007', '9:36am', '-0.46', '935000'],
 ['BA', '98.31', '6/11/2007', '9:36am', '+0.12', '104800'],
 ['C', '53.08', '6/11/2007', '9:36am', '-0.25', '360900'],
 ['CAT', '78.29', '6/11/2007', '9:36am', '-0.23', '225400']]

# 当然你可以在文件关闭前进行一些操作
In [131]: with open('s.csv') as f:
     ...:     data = csv.reader(f)
     ...:     for i in data:
     ...:         print(i)
     ...:         
['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
['AA', '39.48', '6/11/2007', '9:36am', '-0.18', '181800']
['AIG', '71.38', '6/11/2007', '9:36am', '-0.15', '195500']
['AXP', '62.58', '6/11/2007', '9:36am', '-0.46', '935000']
['BA', '98.31', '6/11/2007', '9:36am', '+0.12', '104800']
['C', '53.08', '6/11/2007', '9:36am', '-0.25', '360900']
['CAT', '78.29', '6/11/2007', '9:36am', '-0.23', '225400']

# 写入文件,writerow:写入一行;writerows:写入多行
# 不论之前的数据类型是什么,一律转换成字符串存入文件
In [151]: data
Out[151]: 
[['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume'],
 ['AA', '39.48', '6/11/2007', '9:36am', '-0.18', '181800'],
 ['AIG', '71.38', '6/11/2007', '9:36am', '-0.15', '195500'],
 ['AXP', '62.58', '6/11/2007', '9:36am', '-0.46', '935000'],
 ['BA', '98.31', '6/11/2007', '9:36am', '+0.12', '104800'],
 ['C', '53.08', '6/11/2007', '9:36am', '-0.25', '360900'],
 ['CAT', '78.29', '6/11/2007', '9:36am', '-0.23', '225400']]
In [152]: with open('t.csv', 'w') as f:
     ...:     csv.writer(f).writerows(data)
     ...:     
In [153]: !nl t.csv  # 注意这里虽然与 s.csv 有区别,但每个元素仍然都是字符串
     1  Symbol,Price,Date,Time,Change,Volume
     2  AA,39.48,6/11/2007,9:36am,-0.18,181800
     3  AIG,71.38,6/11/2007,9:36am,-0.15,195500
     4  AXP,62.58,6/11/2007,9:36am,-0.46,935000
     5  BA,98.31,6/11/2007,9:36am,+0.12,104800
     6  C,53.08,6/11/2007,9:36am,-0.25,360900
     7  CAT,78.29,6/11/2007,9:36am,-0.23,225400

# 在 Windows 中写入 csv 文件会出现空行,加个参数 newline='' 即可解决
In [192]: with open('t.csv', 'w', newline='') as f:
     ...:     csv.writer(f).writerows(data)
     ...:  



作者:Manchangdx
链接:https://www.jianshu.com/p/fdaaba28016a
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

你可能感兴趣的:(Python 文件的读写和 json 、csv 的用法)