Printing a random csv cell with string and inserted variable in Python 2

I am a python noob, so please take it easy on me.

I have an example csv file (actual csv file has 20 rows and 2 columns similar to what is shown below):

"I hate %s" % x, "I am a %s" % x
"I heart %s" % x, "I am not a %s" % x

My python 2.7 script:

from csv import *

x = "gorillas"

with open('csv_test.csv', 'rU') as csvfile:
    spamreader = reader(csvfile, quoting = QUOTE_MINIMAL)
    list = []
    for row in spamreader:
        list.append(row[0])

    print list[1]   

csvfile.close()

I would like my script to print:

I heart gorillas

instead it is printing:

"I heart %s" % x

So, my variable, x, is not being inserted into my string. I assume that my problem is that when I pull the contents of the cell from my csv, the whole cell content is considered a string. However, I do not know how to fix this issue.

As a bonus or follow-up, I would also like to be selecting a random cell from my csv file.

Thanks for the help.


One option could be to let the csv file be interpreted as a string format code line. That would require you to interpret the string as a part of the script. You can do this with eval()

print eval(list[1])

ought to do it.

Depending on you application, eval can be useful, but generally I would not recommend reading an input from somewhere, and then run the contents using eval. consider the thought experiment where the text is posted by a user on a website. If the post contains valid python code, they have just gained access to running their own scripts on your machine.

Instead, you could replace parts of the string. and then loose the '% x' format specifier

from csv import *

x = "gorillas"

with open('csv_test.csv', 'rU') as csvfile:
    spamreader = reader(csvfile, quoting = QUOTE_MINIMAL)
    list = []
    for row in spamreader:
        list.append(row[0])

    print list[1].replace("%s", x)   

csvfile.close()

你可以使用eval来做到这一点:

from csv import *

x = "gorillas"

with open('csv_test.csv', 'rU') as csvfile:
    spamreader = reader(csvfile, quoting = QUOTE_MINIMAL)
    list = []
    for row in spamreader:
        list.append(eval(row[0]))

    print list[1]   

csvfile.close()

There are two issues I saw when running the code -

  • You are not setting the quoting to QUOTE_NONE in your reader which causes the csv reader to not replace the double double quotes (atleast it didn't in my machine) , with QUOTE_MINIMAL (in my machine , both python 3.4 and python 2.7) it was replacing the double quotes complete and in the list there was no double quotes.

  • When printing the strings, you have to evaluate the expression and print it = using eval() function.

  • The code would look something like -

    >>> with open('csv_test.csv', 'rU') as csvfile:
    ...     spamreader = reader(csvfile, quoting = QUOTE_NONE)
    ...     list = []
    ...     for row in spamreader:
    ...         list.append(row[0])
    ... 
    >>> print(eval(list[1]))
    I heart gorillas
    

    I am just using | since it does not occur in your csv example, you can use anything.

    Please note its not good practice to keep such statements in a csv and then evaluating them in your function using eval because they can write anything into the csv, and can cause potential issues for your program.

    链接地址: http://www.djcxy.com/p/55098.html

    上一篇: 如何将csv保存到txt中,并搜索记录

    下一篇: 在Python 2中打印带有字符串和插入变量的随机csv单元格