将多行列表转换为一个行列表并替换几个字符

时间:2019-01-21 07:41:19

标签: python

我有一个看起来像这样的列表

['',
 '',
 "'331d5eb313daaffcec36060a8989922618188173', '67cf6e565589f551a3541543bd3e284deceea35a', '34be58d69753136d482bead3e2a8a47136b4dcbd', '38f7d66110261bd246f440a353b54e84e37212d8', '681104a60d035bb36611f59344f51c07fc4169e3', '2b0c52633108d0bdcebc090f0178dddcdf55a901', '8b2d068819359609bdca3312403fc17aea36b7bb', '06eac3c3424c9df828833c7056e14e55bc0577b2', '176d78a6a0115f188da7c2f294fbcb9111c4bb08', 'ab560e53188e3c243b00cb733d788da4bfb6545f', '76542bd9e983651912149d52cf9a76ed09a72eb5', '7a0534ecc7b5910ba35194f730e18a596a1ea8c1', 'a4e1a5632518995a1ee78052453ec48890b60ce9', '517354c67fb73d0762ded89556d2dffb3ca85905', 'e5168a11812069031d6322492fa5b802d8505a48', 'a62036cd53db6a00b4a7edcee6324891dc01279b', 'bf2645810253a1ac727ee71a9c74e407bccc5bf7', '9b92ee50c42d4c113af4c695938b18cc58973080', '48fc209b510c196df6a7924782e60f1120502b0a'",
 "'331d5eb313daaffcec36060a8989922618188173', '67cf6e565589f551a3541543bd3e284deceea35a', '34be58d69753136d482bead3e2a8a47136b4dcbd', '38f7d66110261bd246f440a353b54e84e37212d8', '681104a60d035bb36611f59344f51c07fc4169e3', '2b0c52633108d0bdcebc090f0178dddcdf55a901', '8b2d068819359609bdca3312403fc17aea36b7bb', '06eac3c3424c9df828833c7056e14e55bc0577b2', '176d78a6a0115f188da7c2f294fbcb9111c4bb08', 'ab560e53188e3c243b00cb733d788da4bfb6545f', '76542bd9e983651912149d52cf9a76ed09a72eb5', '7a0534ecc7b5910ba35194f730e18a596a1ea8c1', 'a4e1a5632518995a1ee78052453ec48890b60ce9', '517354c67fb73d0762ded89556d2dffb3ca85905', 'e5168a11812069031d6322492fa5b802d8505a48', 'a62036cd53db6a00b4a7edcee6324891dc01279b', 'bf2645810253a1ac727ee71a9c74e407bccc5bf7', '9b92ee50c42d4c113af4c695938b18cc58973080', '48fc209b510c196df6a7924782e60f1120502b0a'"]

我想使其成为一个单行列表并替换几个字符,以便获得以下输出,

['',
 '',
 '331d5eb313daaffcec36060a8989922618188173', '67cf6e565589f551a3541543bd3e284deceea35a', '34be58d69753136d482bead3e2a8a47136b4dcbd', '38f7d66110261bd246f440a353b54e84e37212d8', '681104a60d035bb36611f59344f51c07fc4169e3', '2b0c52633108d0bdcebc090f0178dddcdf55a901', '8b2d068819359609bdca3312403fc17aea36b7bb', '06eac3c3424c9df828833c7056e14e55bc0577b2', '176d78a6a0115f188da7c2f294fbcb9111c4bb08', 'ab560e53188e3c243b00cb733d788da4bfb6545f', '76542bd9e983651912149d52cf9a76ed09a72eb5', '7a0534ecc7b5910ba35194f730e18a596a1ea8c1', 'a4e1a5632518995a1ee78052453ec48890b60ce9', '517354c67fb73d0762ded89556d2dffb3ca85905', 'e5168a11812069031d6322492fa5b802d8505a48', 'a62036cd53db6a00b4a7edcee6324891dc01279b', 'bf2645810253a1ac727ee71a9c74e407bccc5bf7', '9b92ee50c42d4c113af4c695938b18cc58973080', '48fc209b510c196df6a7924782e60f1120502b0a',
 '331d5eb313daaffcec36060a8989922618188173', '67cf6e565589f551a3541543bd3e284deceea35a', '34be58d69753136d482bead3e2a8a47136b4dcbd', '38f7d66110261bd246f440a353b54e84e37212d8', '681104a60d035bb36611f59344f51c07fc4169e3', '2b0c52633108d0bdcebc090f0178dddcdf55a901', '8b2d068819359609bdca3312403fc17aea36b7bb', '06eac3c3424c9df828833c7056e14e55bc0577b2', '176d78a6a0115f188da7c2f294fbcb9111c4bb08', 'ab560e53188e3c243b00cb733d788da4bfb6545f', '76542bd9e983651912149d52cf9a76ed09a72eb5', '7a0534ecc7b5910ba35194f730e18a596a1ea8c1', 'a4e1a5632518995a1ee78052453ec48890b60ce9', '517354c67fb73d0762ded89556d2dffb3ca85905', 'e5168a11812069031d6322492fa5b802d8505a48', 'a62036cd53db6a00b4a7edcee6324891dc01279b', 'bf2645810253a1ac727ee71a9c74e407bccc5bf7', '9b92ee50c42d4c113af4c695938b18cc58973080', '48fc209b510c196df6a7924782e60f1120502b0a']

我该怎么做?我只想删除似乎双引号。

3 个答案:

答案 0 :(得分:0)

您有一个包含2个空字符串和2个非常长的字符串的列表(在"..."内,因为它们包含'。长字符串看起来像是文本表示形式的列表-没有{{1} } + [

您可以使用简单的for循环和list.extend()方法拆分数据:

]

输出:

text = ['', 
        '', 
        "'331', '67cf', '34b', '38f', '...', '517', '48f'",
        "'331', '67cf', '34b', '38f', '...', '517', '48f'"]

tt = []
for t in text:

    # strip spaces and single quoites from the resulting parts
    spl = [x.strip().strip("'") for x in t.split(",")]
    tt.extend(spl)

print(tt)

答案 1 :(得分:0)

呼叫您的输入list myList。我们使用list comprehensions来解决这个问题。以下代码是通用代码,无论列表中有多少''或双引号的字符串-

myList_out =[[i.replace("'",'') for i in list(k.split(','))] for k in myList]
myList_out = [item for sublist in myList_out for item in sublist]
myList_out
['',
 '',
 '331d5eb313daaffcec36060a8989922618188173',
 ' 67cf6e565589f551a3541543bd3e284deceea35a',
 ' 34be58d69753136d482bead3e2a8a47136b4dcbd',
 ' 38f7d66110261bd246f440a353b54e84e37212d8',
 ' 681104a60d035bb36611f59344f51c07fc4169e3',
 ' 2b0c52633108d0bdcebc090f0178dddcdf55a901',
 ' 8b2d068819359609bdca3312403fc17aea36b7bb',
 ' 06eac3c3424c9df828833c7056e14e55bc0577b2',
 ' 176d78a6a0115f188da7c2f294fbcb9111c4bb08',
 ' ab560e53188e3c243b00cb733d788da4bfb6545f',
 ' 76542bd9e983651912149d52cf9a76ed09a72eb5',
 ' 7a0534ecc7b5910ba35194f730e18a596a1ea8c1',
 ' a4e1a5632518995a1ee78052453ec48890b60ce9',
 ' 517354c67fb73d0762ded89556d2dffb3ca85905',
 ' e5168a11812069031d6322492fa5b802d8505a48',
 ' a62036cd53db6a00b4a7edcee6324891dc01279b',
 ' bf2645810253a1ac727ee71a9c74e407bccc5bf7',
 ' 9b92ee50c42d4c113af4c695938b18cc58973080',
 ' 48fc209b510c196df6a7924782e60f1120502b0a',
 '331d5eb313daaffcec36060a8989922618188173',
 ' 67cf6e565589f551a3541543bd3e284deceea35a',
 ' 34be58d69753136d482bead3e2a8a47136b4dcbd',
 ' 38f7d66110261bd246f440a353b54e84e37212d8',
 ' 681104a60d035bb36611f59344f51c07fc4169e3',
 ' 2b0c52633108d0bdcebc090f0178dddcdf55a901',
 ' 8b2d068819359609bdca3312403fc17aea36b7bb',
 ' 06eac3c3424c9df828833c7056e14e55bc0577b2',
 ' 176d78a6a0115f188da7c2f294fbcb9111c4bb08',
 ' ab560e53188e3c243b00cb733d788da4bfb6545f',
 ' 76542bd9e983651912149d52cf9a76ed09a72eb5',
 ' 7a0534ecc7b5910ba35194f730e18a596a1ea8c1',
 ' a4e1a5632518995a1ee78052453ec48890b60ce9',
 ' 517354c67fb73d0762ded89556d2dffb3ca85905',
 ' e5168a11812069031d6322492fa5b802d8505a48',
 ' a62036cd53db6a00b4a7edcee6324891dc01279b',
 ' bf2645810253a1ac727ee71a9c74e407bccc5bf7',
 ' 9b92ee50c42d4c113af4c695938b18cc58973080',
 ' 48fc209b510c196df6a7924782e60f1120502b0a']

只需进行健全性检查-

len(myList_out)
    40

答案 2 :(得分:0)

这不是多行,您的Gui会像这样显示您。我能看到的是一个列表...而列表就像一个向量或一维数组。

str(yourlist).replace("'", '"')

使用它,希望对您有所帮助