【问题标题】:How to replace value using python format function?如何使用 python 格式函数替换值?
【发布时间】:2019-03-02 14:55:37
【问题描述】:

我有这些数据,我想编写一个 python 程序来替换 csv 中的值。 我的 csv 有以下数据:

name,studentno
abc,student1
xyz,student2
rty,student3
wer,student4

代码:

mystring= "Test {} value : {}"

print (my_string.format(column1value from csv, column2value from csv))

如何循环遍历 csv 并放置列值?请帮忙。我需要如下所示的输出。 输出:

Test abc value : student1
Test xyz value : student2
Test rty value : student3
Test wer value : student4

【问题讨论】:

  • “my_string.format(column1value from csv, column2value from csv)”真的是你唯一的代码尝试吗?这看起来不像是有效的 python 语法...
  • 尝试弄清楚如何遍历 csv 的行并打印每一列的值。一旦你能做到这一点,你的任务就很容易了。不过,恐怕我们不会指导您如何编写for 循环或打开文件。

标签: python python-3.x pandas loops string.format


【解决方案1】:
import csv

mystring= "Test {} value : {}"

with open('yourfil.csv', 'rb') as csvfile:
    csv_reader = csv.reader(csvfile, delimiter=',')
    for row in csv_reader:
        mystring.format(row[0], row[1])

我认为这应该可行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2021-01-10
    • 2020-01-12
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多