Python sửa đổi tập tin tại chỗ

Hãy cho chúng tôi xem cách chúng tôi có thể thay thế giá trị cột của tệp CSV bằng Python. Tệp CSV không là gì ngoài tệp được phân cách bằng dấu phẩy

Phương pháp 1. Sử dụng cách Python gốc

Sử dụng phương thức replace(), chúng ta có thể dễ dàng thay thế một văn bản thành một văn bản khác. Trong đoạn mã dưới đây, hãy để chúng tôi có tệp CSV đầu vào là “csvfile. csv” và được mở ở chế độ “đọc”. Phương thức join() lấy tất cả các dòng của tệp CSV trong một lần lặp và nối chúng thành một chuỗi. Sau đó, chúng ta có thể sử dụng phương thức replace() trên toàn bộ chuỗi và có thể thực hiện thay thế một/nhiều lần. Trong toàn bộ chuỗi, văn bản đã cho được tìm kiếm và thay thế bằng văn bản đã chỉ định

Thí dụ.  

Tệp đầu vào sẽ là

Python3

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 7_______28 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 9____30import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 1import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 2import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 3import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 4

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 7import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 2import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 3 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 4import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 5 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 6

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 7______28 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 9____270import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 2import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 72import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 73

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 7import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 9import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 77import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 2import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 79import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 73

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 7_______28 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 9____284____32import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 86import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 73

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 7import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 9import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 91import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 2import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 93import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 73

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 95import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 9import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 0import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 99import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 2import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 01import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 4

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 03

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 04

đầu ra

Phương pháp 2. Sử dụng Pandas DataFrame

Chúng tôi có thể đọc tệp CSV dưới dạng DataFrame và sau đó áp dụng phương thức thay thế ()

Python3

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 05 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 06

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 07import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 09____310import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 73

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 12import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 14import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 15

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 16______317import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 19import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 15

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 21import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 22import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 24import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 4

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 12import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 28import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 15

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 16import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 17import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 33import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 15

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 21import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 22import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 24import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 4

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 12import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 42import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 15

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 16______317import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 47import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 15

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 21import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 22import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 24import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 4

________ 004 ________ 005 ________ 315

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 16______008import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 8 import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 10import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 4

đầu ra


Một thay thế cho câu trả lời được chấp nhận là

  • Sử dụng import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 12 với import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 13 để sửa đổi tệp tại chỗ
  • Sử dụng import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 14 để truy cập cột qua tiêu đề thay vì chỉ mục (Điều này chỉ hoạt động nếu CSV có tiêu đề)

CSV thử nghiệm

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 6

Mã kiểm tra

import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]]))

Sự khác biệt chính là bạn không phải mở tệp đầu vào theo cách thủ công và tạo tệp đầu ra, vì import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 13 đã làm điều đó ở hậu trường

Lọc tại chỗ tùy chọn. nếu đối số từ khóa import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 13 được chuyển đến import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 17 hoặc hàm tạo import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 18, tệp sẽ được chuyển đến tệp sao lưu và đầu ra tiêu chuẩn được chuyển hướng đến tệp đầu vào (nếu tệp cùng tên với tệp sao lưu đã tồn tại, nó sẽ là . Điều này giúp có thể viết một bộ lọc ghi lại tệp đầu vào của nó tại chỗ

Vòng lặp đi qua từng hàng CSV (ngoại trừ hàng tiêu đề), vì vậy bạn có thể thực hiện bất kỳ thao tác xử lý nào bạn cần trên mỗi hàng

Nếu bạn vẫn muốn giữ lại bản gốc, bạn có thể chuyển vào import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 19 để import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 12 tạo bài kiểm tra. csv. tập tin sao lưu

Ngoài ra, lưu ý rằng với chỉnh sửa tại chỗ, "đầu ra tiêu chuẩn được chuyển hướng đến tệp đầu vào", vì vậy import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 21 in nó ra tệp thay vì dòng lệnh. Nếu bạn muốn thực sự in ra bàn điều khiển, bạn cần chỉ định import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 22 như trong import fileinput with fileinput.input(files=('test.csv'), inplace=True, mode='r') as f: reader = csv.DictReader(f) print(",".join(reader.fieldnames)) # print back the headers for row in reader: if row["Ip"] == "127.0.0.2": row["Sites"] = "30" print(",".join([row["Ip"], row["Sites"]])) 23

Làm cách nào để thay đổi giá trị cột trong Python?

Cách cập nhật hàng và cột bằng Python Pandas .

Tạo một khung dữ liệu Pandas. Trong toàn bộ hướng dẫn này, chúng tôi sẽ sử dụng một khung dữ liệu mà chúng tôi sẽ tạo ngay bây giờ. .

Cập nhật cột. .

Cập nhật trường hợp tên cột. .

Cập nhật giá trị hàng. .

Cập nhật hàng và cột dựa trên điều kiện

Làm cách nào để chỉnh sửa tệp CSV hiện có bằng Python?

Việc chỉnh sửa nội dung của tệp CSV hiện có sẽ yêu cầu các bước sau. đọc dữ liệu trong tệp CSV, chỉnh sửa danh sách (Cập nhật thông tin, thêm thông tin mới, xóa thông tin), sau đó ghi dữ liệu mới trở lại tệp CSV.

Làm cách nào để chỉnh sửa các cột trong tệp CSV?

Mở tệp CSV trong Microsoft Excel hoặc một ứng dụng tương thích, chẳng hạn như trình soạn thảo văn bản hoặc Notepad. Di chuyển con trỏ của bạn đến một dòng trống và nhập H vào cột A. Nhấn phím Tab để di chuyển đến cột tiếp theo và nhập giá trị mà bạn muốn nhập cho trường đó. Lặp lại bước b cho tất cả các trường trong hàng

Chủ đề