Vấn đề của tôi là như sau: Tôi có một tệp CSV với các dòng thường bắt đầu với '200'. Trong tập tin này có các dòng không mong muốn.
I have a csv-file with lines that normally start with '200'. In this file there are unwanted linebreaks.
E.g.
200 Peter Pan
200 John Smith
200 Susan Murray
200 Harald
Williams
200 Liam Noah
Đó là cách mà tệp nên xem xét kết thúc:
200 Peter Pan
200 John Smith
200 Susan Murray
200 Harald Williams
200 Liam Noah
Vì vậy, bất cứ khi nào một dòng không bắt đầu với '200', nó nên được thêm vào dòng trước. Tôi hy vọng điều này sẽ khá dễ dàng với Python nhưng tôi không hiểu đúng.
Mel
5.56810 Huy hiệu vàng39 Huy hiệu bạc42 Huy hiệu đồng10 gold badges39 silver badges42 bronze badges
Hỏi ngày 25 tháng 10 năm 2017 lúc 8:17Oct 25, 2017 at 8:17
3
with open[, 'r+'] as file:
text = str[];
for line in file:
if line[0:3] == "200":
text = "{}\n{}".format[text, line.strip[]];
else:
text = "{} {}".format[text, line.strip[]];
file.seek[0];
file.write[text[1:]];
Đã trả lời ngày 25 tháng 10 năm 2017 lúc 8:24Oct 25, 2017 at 8:24
6
Mã sau đây sẽ thực hiện công việc ...
Với một file
được gọi là file.csv
, với nội dung:
200 Peter Pan
200 John Smith
200 Susan Murray
200 Harald
Williams
200 Liam Noah
Sau khi chúng tôi chạy các ____10 sau:
lines = open["file.csv", "r"].read[].split["\n"]
for i, line in enumerate[lines]:
if not line.startswith["200"]:
lines[i-1] = lines[-1].strip[] + " " + line
lines.pop[i]
open["file.csv", "w"].write["\n".join[lines]+"\n"]
file
được cập nhật như bạn muốn:
200 Peter Pan
200 John Smith
200 Susan Murray
200 Harald Williams
200 Liam Noah
Làm thế nào nó hoạt động?
Các bước:
- Đọc trong tệp
2 dưới dạng200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
3 và chuyển đổi nó thành200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
4 của200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
5 bằng200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
6200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
3 trên ký tự dòng mới [200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
8].200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
- lặp qua
9200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
5200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
4 vì vậy chúng tôi có hai200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
2 để làm việc với:with open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
3LINES.with open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
- Kiểm tra xem
4 bắt đầu bằngwith open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
5.with open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
- Nếu có,
6with open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
4 chowith open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
4with open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
9 trước đó [bởiwith open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
0200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
4 trước và thêm khoảng trống giữa], sau đó loại bỏwith open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
4 khỏiwith open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
4 của200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
5 bằng200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
5.200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
- Cuối cùng, chúng tôi
7 giống nhau200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
2200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
file
cho
0 vàlines = open["file.csv", "r"].read[].split["\n"] for i, line in enumerate[lines]: if not line.startswith["200"]: lines[i-1] = lines[-1].strip[] + " " + line lines.pop[i] open["file.csv", "w"].write["\n".join[lines]+"\n"]
1 cho nólines = open["file.csv", "r"].read[].split["\n"] for i, line in enumerate[lines]: if not line.startswith["200"]: lines[i-1] = lines[-1].strip[] + " " + line lines.pop[i] open["file.csv", "w"].write["\n".join[lines]+"\n"]
5 mới.200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
3 từ200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
4 của200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
5 có bằng cách thêm một ký tự dòng mới giữa mỗi200 Peter Pan 200 John Smith 200 Susan Murray 200 Harald Williams 200 Liam Noah
4 và cuối cùng thêm một ký tự vào cuối.with open[, 'r+'] as file: text = str[]; for line in file: if line[0:3] == "200": text = "{}\n{}".format[text, line.strip[]]; else: text = "{} {}".format[text, line.strip[]]; file.seek[0]; file.write[text[1:]];
Hy vọng điều này sẽ giúp bạn ra ngoài!
Đã trả lời ngày 25 tháng 10 năm 2017 lúc 10:52Oct 25, 2017 at 10:52
Joe Iddonjoe IddonJoe Iddon
Huy hiệu vàng 19,6K77 gold badges32 silver badges51 bronze badges
Mã hoạt động hoàn toàn tốt đối với tôi là như sau:
with open['testing2.CSV', 'r+', encoding="utf-8"] as file:
text = str[];
for line in file:
if line[0:3] == "200":
text = text + '\n';
text = text + line.strip[];
file.seek[0];
file.write[text];
Nó thậm chí còn giữ dòng đầu tiên tốt đẹp vì tệp CSV của tôi có tiêu đề. Cảm ơn tất cả những người đã giúp đỡ ở đây, đặc biệt là Benjamin James Drury và Joe Iddon.
Đã trả lời ngày 25 tháng 10 năm 2017 lúc 11:45Oct 25, 2017 at 11:45
ShortDiveshortDiveShortDive
1571 Huy hiệu vàng2 Huy hiệu bạc11 Huy hiệu đồng1 gold badge2 silver badges11 bronze badges
Đọc tệp CSV và lặp qua các dòng:
with open['test.csv', 'r'] as f:
f_csv = csv.reader[f]
# header = next[f_csv]
for row in f_csv:
if not row.startswith[200]:
#append previous row
Đã trả lời ngày 25 tháng 10 năm 2017 lúc 8:23Oct 25, 2017 at 8:23
metmirrmetmirrmetmirr
4.1192 Huy hiệu vàng20 Huy hiệu bạc34 Huy hiệu đồng2 gold badges20 silver badges34 bronze badges