python怎么换行输出单引号_如何在python中替换列表中的单引号

I have a list:

my_list = ['"3"', '"45"','"12"','"6"']

This list has single and double quotes and the item value. How can I replace either the single or double quotes from each item. I tried below, but the results are same:

my_list = [i.replace("''", " ") for i in my_list]

解决方案

Your list doesn't contain any strings with single quotes. I think you are confusing the repr() representation of the strings with their values.

When you print a Python standard library container such as a list (or a tuple, set, dictionary, etc.) then the contents of such a container are shown their repr() representation output; this is great when debugging because it makes it clear what type of objects you have. For strings, the representation uses valid Python string literal syntax; you can copy the output and paste it into another Python script or the interactive interpreter and you'll get the exact same value.

For example, s here is a string that contains some text, some quote characters, and a newline character. When I print the string, the newline character causes an extra blank line to be printed, but when I use repr(), you get the string value in Python syntax form, where the single quotes are part of the syntax, not the value. Note that the newline character also is shown with the \n syntax, exactly the same as when I created the s string in the first place:

>>> s = 'They heard him say "Hello world!".\n'

>>> print(s)

They heard him say "Hello world!".

>>> print(repr(s))

'They heard him say "Hello world!".\n'

>>> s

'They heard him say "Hello world!".\n'

And when I echoed the s value at the end, the interactive interpreter also shows me the value using the repr() output.

So in your list, your strings do not have the ' characters as part of the value. They are part of the string syntax. You only need to replace the " characters, they are part of the value, because they are inside the outermost '...' string literal syntax. You could use str.replace('"', '') to remove them:

[value.replace('"', '') for value in my_list]

or, you could use the str.strip() method to only remove quotes that are at the start or end of the value:

[value.strip('"') for value in my_list]

Both work just fine for your sample list:

>>> my_list = ['"3"', '"45"','"12"','"6"']

>>> [value.replace('"', '') for value in my_list]

['3', '45', '12', '6']

>>> [value.strip('"') for value in my_list]

['3', '45', '12', '6']

Again, the ' characters are not part of the value:

>>> first = my_list[0].strip('"')

>>> first # echo, uses repr()

'3'

>>> print(first) # printing, the actual value written out

3

>>> len(first) # there is just a single character in the string

1

However, I have seen that you are reading your data from a tab-separated file that you hand-parse. You can avoid having to deal with the " quotes altogether if you instead used the csv.reader() object, configured to handle tabs as the delimiter. That class automatically will handle quoted columns:

import csv

with open(inputfile, 'r', newline='') as datafile:

reader = csv.reader(datafile, delimiter='\t')

for row in reader:

# row is a list with strings, *but no quotes*

# e.g. ['3', '45', '12', '6']

Demo showing how csv.reader() handles quotes:

>>> import csv

>>> lines = '''\

... "3"\t"45"\t"12"\t"6"

... "42"\t"81"\t"99"\t"11"

... '''.splitlines()

>>> reader = csv.reader(lines, delimiter='\t')

>>> for row in reader:

... print(row)

...

['3', '45', '12', '6']

['42', '81', '99', '11']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值