[Python] Read and Parse Files in Python
Python in and Read Files parse
2023-09-14 09:00:51 时间
This lesson will teach you how to read the contents of an external file from Python. You will also learn how to use the python csv module to read and parse csv data, as well as the json module to read and parse json data.
# f = open('animals.csv') # for line in f: # print(line) # f.close() # with open('animals.csv', 'r') as f: # print(f.read()) # import csv # with open('animals.csv', 'r') as f: # animals = csv.reader(f) # for row in animals: # if row[-1] == 'True': # print("{0} is a {1} and is housebroken".format(row[0], row[1])) # elif row[-1] == 'False': # print("{0} is a {1} and is not housebroken!".format(row[0], row[1])) # import json # with open('animals.json', 'r') as r: # data = json.load(r) # for row in data: # print(row['name']) # f = open('cars.txt', 'a') # cars = ['chevy', 'tesla', 'ford'] # for car in cars: # f.write(car + '\n') # f.close() # with open('cars.txt', 'a') as f: # cars = ['chevy', 'vw', 'mazda'] # for car in cars: # f.write(car + '\n') cars = [ {"make": "chevy"}, {"make": "tesla"}, {"make": "porsche"} ] import json with open('cars.json', 'w') as f: json.dump(cars, f)
相关文章
- Python学习--13 文件I/O
- python 中hive 取日期时间的方法
- Python类属性的延迟计算
- [Python] Format Strings in Python
- [Python] Understand Mutable vs. Immutable objects in Python
- [Python] Read and Parse Files in Python
- [Python] Find available methods and help in REPL
- Python version 2.7 required, which was not found in the registry解决方法
- [Python] Find available methods and help in REPL
- Python version 2.7 required, which was not found in the registry解决方法
- Python 刷Leetcode题库,顺带学英语单词(7)
- Atitit.异步编程 java .net php python js 的比较
- 华为OD机试 - 打印机队列(Java & JS & Python)
- 成功解决File "f:program filespythonpython36libre.py", line 142, in <modul
- 成功解决Command "python setup.py egg_info" failed with error code 1 in C:UsersAppData
- python之hex bytes转str
- python实现服务器(亲测可用,可关闭)
- Python数据分析与展示:pandas算术和比较运算-11
- machine learning in coding(python):使用贪心搜索【进行特征选择】
- [转]Python UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法
- python中,用Redis构建分布式锁