简体   繁体   中英

How to replace value using python format function?

I have these data and I want to write a python program to replace the value from csv. My csv has below data:

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

Code:

mystring= "Test {} value : {}"

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

How can I loop through csv and put the column values? Please help. I need Output as shown below. Output:

Test abc value : student1
Test xyz value : student2
Test rty value : student3
Test wer value : student4
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])

I think this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM