如何在csvfi中的特定列上转储数字序列

2024-09-28 18:58:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在现有数据文件中为特定列生成一个数字序列。有没有人能分享一下你对如何用python实现这一点的看法。非常感谢。你知道吗

Input :-

1,A,X
2,B,X
3,C,X

Output :-

1,A,101
2,B,102
3,C,103

Tags: inputoutput数据文件序列数字人能
1条回答
网友
1楼 · 发布于 2024-09-28 18:58:37

使用csv库:

import csv

with open('file.csv', 'rb') as csv_file_to_read, open('file2.csv', 'wb') as csv_file_to_write:
    csv_content = csv.reader(csv_file_to_read)
    csv_writer = csv.writer(csv_file_to_write, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    csv_content = [_ for _ in csv_content]

    for i in range(csv_content):
        csv_content[i].insert(0, i)
        csv_content[i].append(100 + i)

    csv_writer.writerow(csv_content)

相关问题 更多 >