简体   繁体   English

删除unicode [u']

[英]Remove unicode [u']

I have this string, input from a webpage. 我有这个字符串,是从网页输入的。

s = "[u'967208', u'411600', u'460273']"

I want to remove the brackets [ ] and u and ' . 我要删除括号[ ]以及u'

I would also like to make new line breaks instead of the commas , . 我也想提出new line breaks ,而不是逗号,

I have spent much time searching for a solution, including encoding and regex, but I can't seem to get it working. 我花了很多时间寻找解决方案,包括编码和正则表达式,但似乎无法正常工作。

Updated : This is what I use to retrieve the string: 更新 :这是我用来检索字符串的方法:

import selenium
import re
input = webdriver.find_element_by_class_name("class_name")
s = re.findall("((?<=\()[0-9]*)", input.text)
>>> import ast
>>> s = "[u'967208', u'411600', u'460273']"
>>> a = ast.literal_eval(s)
>>> print(*a, sep='\n')
967208
411600
460273

If you just want the digits with re just use \\d+ : 如果您只想使用re进行数字操作,请使用\\d+

import re

s = "[u'967208', u'411600', u'460273']"
print "\n".join(re.findall(r"\d+", s))
967208
411600
460273

It is safe and efficient: 安全高效:

In [7]: timeit "\n".join(literal_eval(s))
100000 loops, best of 3: 11.7 µs per loop

In [8]: r = re.compile(r"\d+")

In [9]: timeit "\n".join(r.findall(s))
1000000 loops, best of 3: 1.35 µs per loop

If your goal is to write each string to a file, you can use the csv module to write the list of strings returned from re.findall, using a newline as the delimiter: 如果您的目标是将每个字符串写入文件,则可以使用csv模块使用换行符作为定界符,以写入从re.findall返回的字符串列表:

s = u"[u'967208', u'411600', u'460273']"

import csv
import re
with open("out.csv","w") as out:
    wr = csv.writer(out,delimiter="\n")
    r = re.compile("\d+")
    wr.writerow(r.findall(s))

Output: 输出:

967208
411600
460273

If you have many strings just iterate calling call r.findall and pass the result to writerow. 如果您有很多字符串,只需迭代调用r.findall并将结果传递给writerow。

I think after the comments the mystery is solved, you had a list of digits all along that was returned from your regex using findall so you can do the following: 我认为在解开谜题之后,您一直使用findall从正则表达式返回数字列表,因此您可以执行以下操作:

u"abc(967208) \n abc2(411600) \n abc3(460273)" # input.text

import csv
import re
with open("out.csv","w") as out:
    wr = csv.writer(out,delimiter="\n")
    r = re.compile("\((\d+)\)")
    wr.writerow(r.findall(input.text))

\\((\\d+)\\) will find 1+ digits inside parens. \\((\\d+)\\)将在括号内找到1个以上的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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