Technology Sharing

Python - Read and write csv files

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Table of contents

csv library method parameters

Reading Data

csv.reader method

Reading operation of specified row or column data in a file

txt file readlines, read method

csv.DictReader Methods

data input

txt file write, writelines

csv.writer Method

csv.DictWriter Methods

Read and write joint (modify and insert data)


When reading and writing csv files, you usually need to deal with issues such as file path, open mode, character encoding, etc. The newline='' parameter is usually required when reading and writing csv files to ensure that the reading and writing of files will not be affected by the line break character of the Windows system. Txt files and csv files can be converted to each other, so the reading and writing of txt files are completely applicable to csv files.

csv library method parameters

parameterdefault valuedescribe
delimiter,It refers to the character used to separate values ​​(or fields) in a CSV file.
skipinitialspaceFALSEIt controls how whitespace following the delimiter is interpreted. If True, initial whitespace will be removed.
lineterminatorrnIt refers to the character sequence used to terminate a line.
quotechar"It refers to a single string that will be used to quote values ​​if special characters (such as delimiters) appear in the field. For example, for "0,0" contains ",", you can use quotechar=""" to treat the data in "" as a whole. If you use '0,0', just use quotechar="'"
quotingcsv.QUOTE_NONEControls when quotes are generated by the author or recognized by the reader (see above for other options).
escapecharNoneWhen quoting is set to quote, it is used to escape the one-character string of delimiters.
doublequoteTRUEControls the handling of quotes within fields.whenTrueWhen doublequote is set, two consecutive quotes are interpreted as one during reading, and each quote character embedded in the data is written as two quotes when writing. By default, doublequote is set to True. As a result, two consecutive double quotes are interpreted as one when read. If doublequote is set to False, consecutive double quotes will appear in the output.

Reading Data

csv.reader method

  1. import csv
  2. with open(file_name, 'r', encoding='utf-8-sig') as f:
  3. reader = csv.reader(f) # 创建csv阅读器对象,读取所有有效数据,返回结果为一个迭代器类对象
  4. for data in reader: # 遍历每一行的数据
  5. print(data)

encoding can specify the encoding format as utf-8-sig, which can automatically process BOM characters and eliminate the appearance of ufeff.

Reading operation of specified row or column data in a file
  1. with open(file_name, 'r', encoding='utf-8-sig') as f:
  2. reader = csv.reader(f)
  3. list_csv = list(reader)
  4. for i in range(len(list_csv)-5,len(list_csv)): # 如读取后5行数据
  5. print(list_csv[i])
  6. print(list_csv[i][:3]) # 读取指定列数据

txt file readlines, read method

Use readlines() and read() to read txt files. There are line breaks in the read data that need to be processed. No need to import csv library

  1. with open(file_name, 'r', encoding='utf-8-sig') as f:
  2. reader_lines = f.readlines() # 读取所有行放在一个列表中
  3. for da in reader_lines: # 也可以使用range方法读取指定行的数据,读取结果中有换行符需要处理
  4. print(da.replace('n',''))
  5. reader = f.read()
  6. print(reader)

csv.DictReader Methods

Read data in dictionary form, and print out data in the form of dictionary key-value pairs.

  1. with open(file_name,'r', encoding='utf-8-sig',newline='') as f:
  2. reader =csv.DictReader(f)
  3. for r in reader:
  4. print(r)

data input

txt file write, writelines

No need to import csv library

Use write and writelines of txt file to write data. When using, English comma in characters is recognized as horizontal tabulation character, and line break character is recognized as vertical tabulation character, enter, to wrap lines. If you want to insert a blank line, write data parameter with line break character. If you want to write to an empty cell, write data parameter with English comma.

  1. with open(file_name, 'w', newline='') as f:
  2. f.write('测试写入数据操作n')
  3. f.write('n')
  4. f.writelines(["角色管理,测试测试", "账号管理n", "部门管理n"])

  

csv.writer Method

When writing data, a variable can be accepted, which can be a string, list, tuple, set, dictionary or other traversable object [string will fill each character into the cell separately, dictionary will traverse and write the key]. writerow writes a single line of data, writerows writes multiple lines of data.

  1. with open(file_name, 'w', newline='') as f:
  2. ws = csv.writer(f) # 创建一个写入文件对象
  3. ws.writerow([]) # 列表内容为空,插入的是一个空行
  4. ws.writerow(["设备实时监控", "设备数据列表", "设备报警分析"]) # 列表数据
  5. ws.writerow(("角色管理", "账号管理", "部门管理")) # 元组数据
  6. write_data = ['贾史王薛',
  7. ['贾不假,白玉为堂金作马', '阿房宫,三百里,住不下金陵一个史', '东海缺少白玉床,龙王请来金陵王',
  8. '丰年好大雪,珍珠如土金如铁'], ('cao', 'xue', 'qin', 'shu'), {'曹', '雪', '芹', '书'},
  9. {'贾': '宝玉', '史': '湘云', '王': '熙凤', '薛': '宝钗'},
  10. {'贾': '宝玉', '史': '湘云', '王': '熙凤', '薛': '宝钗'}.values()]
  11. ws.writerows(write_data) # writerows写入多行数据

csv.DictWriter Methods

When DictWriter writes data, it needs to determine whether the key is in the fieldnames based on the dictionary key. If it does not exist, an error will be reported. If you need to add all known and unknown dictionary data, you can first read all the keys of the data to be written, compare them with the original file header (read the original file header), add it to the header list, and then write the data.

  1. fieldnames = ['姓氏', '人物', '说明'] # 表头
  2. dictate = [{'姓氏': '贾', '人物': "宝玉"}, {'姓氏': '史', '人物': "湘云"}, {'姓氏': '王', '人物': "熙凤"},
  3. {'姓氏': '薛', '人物': "宝钗"}]
  4. with open(file_name, 'w', newline='') as f:
  5. write = csv.DictWriter(f, fieldnames=fieldnames, delimiter=',')
  6. write.writeheader() # 写入表头
  7. write.writerow({'姓氏': '贾', '人物': "宝玉"}) # 单行模式写入
  8. write.writerows(dictate) # 多行模式写入

Read and write joint (modify and insert data)

Principle: First read the original file data, transfer it to a list for storage, then use the list method to perform operations such as addition and deletion based on the obtained list, and finally write the modified list data into the file using multi-line writing.

  1. # 第一步:读取数据
  2. with open(file_name, 'r+', newline='') as f:
  3. file_csv_data = list(csv.reader(f))
  4. # 第二步:操作数据
  5. # 插入数据
  6. file_csv_data.insert(2, '贾史王薛') # 在某行插入一行数据,如在第3行插入数据
  7. file_csv_data[2].insert(0, '红楼四大家族') # 在指定单元格插入数据,如在3行1列的单元格插入数据
  8. # 修改数据
  9. file_csv_data[2] = ['贾不假,白玉为堂金作马', '阿房宫,三百里,住不下金陵一个史', '东海缺少白玉床,龙王请来金陵王',
  10. '丰年好大雪,珍珠如土金如铁', ''] # 修改指定行一行数据
  11. file_csv_data[2][4] = '葫芦僧判葫芦案' # 修改某一单元格数据,如修改3行5列数据
  12. # 修改符合条件的数据数据
  13. # 修改符合要求的单元格数据:通过for循环遍历判断,例如修改单元格中包含1的数据,将1替换成一
  14. # for r_id, row in enumerate(file_csv_data): # 通过行号列号修改
  15. # for c_id, col in enumerate(row):
  16. # if '1' in col:
  17. # file_csv_data[r_id][c_id] = col.replace('1', '一')
  18. for row in file_csv_data: # 通过行数据及列号修改,比上面更方便
  19. for c_id, col in enumerate(row):
  20. if '1' in col:
  21. row[c_id] = col.replace('1', '一')
  22. # 第三步:写入报存数据
  23. with open(file_name, 'w', newline='') as f:
  24. write_file = csv.writer(f)
  25. write_file.writerows(file_csv_data)