【问题标题】:how to make a unique data from strings如何从字符串中生成唯一数据
【发布时间】:2016-12-21 13:51:08
【问题描述】:

我有这样的数据。字符串用逗号分隔。

"India1,India2,myIndia     "
"Where,Here,Here   "
"Here,Where,India,uyete"
"AFD,TTT"

我想要做的是将它们全部放在一列中(彼此下方)所以它会变成这样

India1
India2
myIndia
Where
Here
Here
Here
Where
India
uyete
AFD
TTT

然后我保留导致这个的独特的那些

India1
India2
myIndia
Where
Here
India
uyete
AFD
TTT

所以我有第一个 .txt 格式的数据,我尝试使用 numpy 来处理这个

这是我的代码

#!/usr/bin/python
import numpy as np

# give a name to my data 
file_name = 'path to my data/test.txt'
# set my output 
with open ( 'output.txt' , 'w' ) as out:
    # read all the lines
    for n , line in enumerate ( open ( file_name ).readlines ( ) ):
        # split each stirg from another one by a comma
        item1 = file_name.split ( ',' )
    myList = ','.join ( map ( str , item1 ) )
    item2 = np.unique ( myList , return_inverse=True )
    # save the data into out
    out.write ( item2 )

我收到了TypeError: expected a character buffer object

我已经搜索过了,我发现了几个类似的帖子 TypeError: expected a character buffer object - while trying to save integer to textfile

如果我添加了out.seek ( 0 ),我仍然会遇到同样的错误

但是通过将其更改为out.write ( str(item2 )) 感谢TypeError: expected a character buffer object,我没有收到任何错误,但是输出显示了这个

(array(['/文件路径/test.txt'], dtype='|S29'), 数组([0]))

下面给出了我尝试使用的解决方案

import csv

data = []
def remove_quotes(file):
    for line in file:
        yield line.strip ( '"\n' )
with open ( 'test.txt' ) as f:
    reader = csv.reader ( remove_quotes ( f ) )
    for row in reader:
        data.extend ( row )

没有错误但也没有生成data

【问题讨论】:

  • 您可以简单地从文件中读取、拆分并将所有内容放在一个集合中。
  • file_name.split ( ',' )。你期望它做什么?
  • 参见itertools 文档的recipes 部分中的unique_everseen
  • @cricket_007 我希望用comma分割字符串
  • @nik 你为什么要拆分文件名?

标签: python


【解决方案1】:

stack.txt 包含以下内容:

"India1,India2,myIndia"
"Where,Here,Here"
"Here,Where,India,uyete"
"AFD,TTT"

给你:

from collections import OrderedDict

with open("stack.txt", "r") as f:
    # read your data in from the gist site and strip off any new-line characters
    data = [eval(line.strip()) for line in f.readlines()]
    # get individual words into a list
    individual_elements = [word for row in data for word in row.split(",")]
    # remove duplicates and preserve order
    uniques = OrderedDict.fromkeys(individual_elements)   
    # convert from OrderedDict object to plain list
    final = [word for word in uniques]

print(final)

产生这个:

['India1', 'India2', 'myIndia', 'Where', 'Here', 'India', 'uyete', 'AFD', 'TTT']

编辑:要获得所需的输出,只需以所需的格式打印列表:

print("\n".join(final))

从输出的角度来看,这相当于:

for x in final:
    print(x)

产生这个:

India1
India2
myIndia
Where
Here
India
uyete
AFD
TTT

【讨论】:

  • 我已经喜欢你的回答了!只是一件事,是否可以将输出作为一列而没有任何,'一个在另一个之下?如果是这样,我接受并喜欢你的回答
  • final 是一个list 对象,因此它使用', 字符分隔其字符串元素。会更新。
【解决方案2】:

为什么要使用 numpy ???而且我不确定您是否要使用相同的文件作为输入和输出

#!/usr/bin/env python


# give a name to my data 
inputData = """India1,India2,myIndia
Where,Here,Here   
Here,Where,India,uyete
AFD,TTT"""

# if you want to read the data from a file
#inputData = open(fileName, 'r').readlines()

outputData = ""
tempData = list()
for line in inputData.split("\n"):
    lineStripped = line.strip()
    lineSplit = lineStripped.split(',')
    lineElementsStripped = [element.strip() for element in lineSplit]
    tempData.extend( lineElementsStripped )
tempData = set(tempData)
outputData = "\n".join(tempData)
print("\nInputdata: \n%s" % inputData)
print("\nOutputdata: \n%s" % outputData)

【讨论】:

  • 维持秩序重要吗?在提供答案之前,您可能应该要求澄清问题。
  • 所有没有明确要求的东西对我来说都不重要。
  • from collections import OrderedDict; tempData = OrderedDict.fromkeys(tempData).keys() 之类的东西应该保留顺序。
  • @not_a_robot 这是什么tempData
  • @JDB 订单对我来说很重要。看我上面的问题。我展示了输出的样子
【解决方案3】:

听起来您可能有一个 csv 文件。你不需要 numpy,随附的电池就是你所需要的。

 import csv

 data = []
 with open('test.txt') as f:
     reader = csv.reader(f)
     for row in reader:
         data.extend(row)

您可以.extend 列出而不是.append。基本上就像在说

for thing in row:
    data.append(thing)

不过,这仍然会留下重复项。如果您不关心订单,您可以将其设为 set 并调用 .update() 而不是扩展:

 data = set()
 with open('test.txt') as f:
     reader = csv.reader(f)
     for row in reader:
         data.extend(row)

现在一切都是独一无二的。但是,如果您关心订单,则必须稍微过滤一下:

unique_data = []
for thing in data:
    if thing not in unique_data:
        unique_data.append(thing)

如果您的test.txt 文件包含此文本:

"India1,India2,myIndia     "
"Where,Here,Here   "
"Here,Where,India,uyete"
"AFD,TTT"

不是

India1,India2,myIndia     
Where,Here,Here   
Here,Where,India,uyete
AFD,TTT

那么你还没有一个 csv。您可以修复生成 csv 的内容,也可以手动删除引号即时修复它。

def remove_quotes(file):
    for line in file:
        yield line.strip('"\n')

reader = csv.reader(remove_quotes(f))

【讨论】:

  • 您的文件是否包含"foo,bar,thing,quux"\n"next,line,goes,here"\n?如果是这样,您需要修复 csv 或包装文件。
  • 是的,我在这里分享一个例子gist.github.com/anonymous/63b1a70e913c1453b0de9d7027b5973a
  • 顺便说一句,引号包含在文件中,显然...... OP 提出了多个包含此数据的问题
  • @nik 那么你肯定想要remove_quotes 包装器。
  • @Wayne Werner 将最后一个remove_quotes?my 放在哪里意味着将 remove_quotes 与您提供的第一个解决方案合并,如何让读者知道?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多