我有一个包含大约2000条记录的CSV文件.
每条记录都有一个字符串和一个类别.
This is the first line, Line1
This is the second line, Line2
This is the third line, Line3
Run Code Online (Sandbox Code Playgroud)
我需要将此文件读入一个看起来像这样的列表;
List = [('This is the first line', 'Line1'),
('This is the second line', 'Line2'),
('This is the third line', 'Line3')]
Run Code Online (Sandbox Code Playgroud)
如何将此导入csv到我需要使用Python的列表中?
我试图从一个长字符串中获取一个URL,我不确定如何编写正则表达式;
$ string = '192.00.00.00 - WWW.WEBSITE.COM GET /random/url/link'
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用're.search'函数,以便仅在没有空格的情况下拔出WWW.WEBSITE.COM.我希望它看起来像这样;
$ get_site = re.search(regex).group()
$ print get_site
$ WWW.WEBSITE.COM
Run Code Online (Sandbox Code Playgroud) 我无法理解为什么我的代码无法正常工作.我试图从列表中删除长度只有一个字符的单词:
line = ['word','a','b','c','d','e','f','g']
for words in line:
if len(words) == 1:
line.remove(words)
Run Code Online (Sandbox Code Playgroud)
此代码返回此(它看起来删除'每隔一个'单个字符):
>>> line
['word', 'b', 'd', 'f']
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么这不正常以及如何解决?
我之前在stackoverflow上看过列表比较的方法,但它们似乎都返回列表的匹配 - 而不是非匹配.我有兴趣找到返回非匹配值的方法(可能还有它们来自哪个列表).
listA = ['spanish', 'english', 'chinese', 'italian', 'english']
listB = ['spanish', 'english', 'italian', 'japanese']
Run Code Online (Sandbox Code Playgroud)
我想至少回复哪些词没有在嘴里找到;
['chinese','japanese']
Run Code Online (Sandbox Code Playgroud)
如果没有这个;
[('chinese',listA),('japanese',listB)]
Run Code Online (Sandbox Code Playgroud)
请提供此比较方法的任何解决方案.