-2

The following code results in a string that I want to remove:

with open('company_inf1.csv', 'rb') as inf :
    reader = csv.reader(inf, delimiter=',')
    reader.next() #skip header
    for i,row in enumerate(reader):
        name, date, indus, nike1, nike2, nike3, paid, abbr = row
        t = (map(str, (nike1, nike2, nike3)),) # map & merge the 3 cols together\
        b = str(t) [1:-1]
        c = b.replace('"', '\\"')
        print( name, date, indus, b , paid, abbr)

My output is

 ('abc', '30-06-1987', 'Service', "['nike1', 'nike2', 'nike3 '],", '100',
 'abs')

I also want to remove the double quotes, so that the output become:

<output missing from OP>  

I have already tried with replace and strip commands, but it did not work for me. I also want to save the test where I am printing the value of b into a test file. How can I do this?

4

3 回答 3

3

要从简单的“字符串”中删除括号,您可以使用:

import re
...
re.sub(r'[\(\)]', '', str(mystring))

但是,如果它是一个数组,你需要使用类似的东西:

p = re.compile(r'[\(\)]')
list = [p.sub(' ', x).strip() for x in array]
于 2018-09-19T08:30:02.787 回答
2

您不需要映射到字符串,因为它从文件中出来时已经是一个字符串。

你可以这样做b = [nike1, nike2, nike3],或者:

with open('company_inf1.csv', 'rb') as inf :
    reader = csv.reader(inf, delimiter=',')
    reader.next() #skip header
    name, date, indus, nike1, nike2, nike3, paid, abbr = row
    print(name, date, indus, [nike1, nike2, nike3], paid, abbr)
于 2013-08-06T06:12:13.127 回答
0

你可以尝试这样的事情:

print (value[0] + value[1] + "(" + value[2] + ")")
于 2013-08-06T07:34:36.080 回答