以下代码会生成我要删除的字符串:
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)
我的输出是
('abc', '30-06-1987', 'Service', "['nike1', 'nike2', 'nike3 '],", '100',
'abs')
我还想删除双引号,以便输出变为:
<output missing from OP>
我已尝试使用replace
和strip
命令,但它对我不起作用。我还想将测试保存在我将b
的值打印到测试文件中的位置。我怎么能这样做?
答案 0 :(得分: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)
答案 1 :(得分:1)
To remove the parenthesis from a simple "string" you can use:
import re
...
re.sub(r'[\(\)]', '', str(mystring))
However, if it's an array, you need to employ something like:
p = re.compile(r'[\(\)]')
list = [p.sub(' ', x).strip() for x in array]
答案 2 :(得分:0)
您可以尝试这样的事情:
print (value[0] + value[1] + "(" + value[2] + ")")