如何创建唯一的列表单元格?

2024-09-29 18:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个txt文件,如下所示,其中包括4行作为示例,每个行字符串用,分隔。在

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

https://gist.github.com/anonymous/cee79db7029a7d4e46cc4a7e92c59c50

文件可以从这里下载

我要提取所有唯一的单元格,输出2

^{pr2}$

如果我把数据称为df,我试着逐行阅读并打印出来

myfile = open("df.txt")
lines = myfile.readlines()
for line in lines:
   print lines

Tags: 文件字符串txt示例dfherewheremyfile
3条回答

如果您的stack.txt文件如下所示(即,它保存为.txt文件):

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

解决方案:

^{pr2}$

要获得所需的列输出:

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

结果是:

India1
India2
myIndia     
Where
Here   
India
uyete
AFD
TTT

我不会给你整个代码,但我会给你一些想法。在

首先,您需要读取文件的所有行:

lines = open("file.txt").readlines()

然后,从每行中提取数据:

^{pr2}$

{cd1>可以生成组合。每行的打印元素组合。在

如果不关心元素的顺序,可以使用set获得唯一元素。在使用set之前,应该先将列表lines展平,可以使用itertools.chain.from_iterable。在

选项1:.csv.txt文件

本机Python无法读取.xls文件。如果将文件转换为.csv.txt,则可以使用标准库中的csv模块:

# `csv` module, Standard Library
import csv

filepath = "./test.csv"

with open(filepath, "r") as f:
    reader = csv.reader(f, delimiter=',')
    header = next(reader)                                  # skip 'A', 'B'
    items = set()
    for line in reader:
        line = [word.replace(" ", "") for word in line if word]
        line = filter(str.strip, line)
        items.update(line)

print(list(items))
# ['uyete', 'NHYG', 'QHD', 'SGDH', 'AFD', 'DNGS', 'lkd', 'TTT']

选项2:.xls.xlsx文件

如果要保留原始的.xls格式,则必须安装third-party module到{a2}。在

从命令提示符安装xlrd

^{pr2}$

在Python中:

# `xlrd` module, third-party
import itertools
import xlrd

filepath = "./test.xls"

with xlrd.open_workbook(filepath) as workbook:
    worksheet = workbook.sheet_by_index(0)                 # assumes first sheet
    rows = (worksheet.row_values(i) for i in range(1, worksheet.nrows))
    cells = itertools.chain.from_iterable(rows)
    items = list({val.replace(" ", "") for val in cells if val})

print(list(items))
# ['uyete', 'NHYG', 'QHD', 'SGDH', 'AFD', 'DNGS', 'lkd', 'TTT']

选项3:数据帧

您可以使用pandas数据帧处理csv和文本文件。See documentation用于其他格式。在

import pandas as pd
import numpy as np

# Using data from gist.github.com/anonymous/a822647a00087abc12de3053c700b9a8
filepath = "./test2.txt"

# Determines columns from the first line, so add commas in text file, else may throw an error
df = pd.read_csv(filepath, sep=",", header=None, error_bad_lines=False)
df = df.replace(r"[^A-Za-z0-9]+", np.nan, regex=True)      # remove special chars    
stack = df.stack()
clean_df = pd.Series(stack.unique())
clean_df

数据帧输出

0     India1
1     India2
2    myIndia
3      Where
4       Here
5      India
6      uyete
7        AFD
8        TTT
dtype: object

另存为文件

# Save as .txt or .csv without index, optional

# target = "./output.csv"
target = "./output.txt"
clean_df.to_csv(target, index=False)

注意:选项1&2的结果也可以用pd.Series(list(items))转换成无序的pandas列式对象。在

最后:作为脚本

将上面三个选项中的任何一个保存在一个名为restack.py的函数(stack)中。将此脚本保存到一个目录。在

# restack.py
import pandas as pd
import numpy as np

def stack(filepath, save=False, target="./output.txt"):
    # Using data from gist.github.com/anonymous/a822647a00087abc12de3053c700b9a8

    # Determines columns from the first line, so add commas in text file, else may throw an error
    df = pd.read_csv(filepath, sep=",", header=None, error_bad_lines=False)
    df = df.replace(r"[^A-Za-z0-9]+", np.nan, regex=True)      # remove special chars    
    stack = df.stack()
    clean_df = pd.Series(stack.unique())

    if save:
        clean_df.to_csv(target, index=False)
        print("Your results have been saved to '{}'".format(target))

    return clean_df

if __name__ == "__main__":
    # Set up input prompts
    msg1 = "Enter path to input file e.g. ./test.txt: "
    msg2 = "Save results to a file? y/[n]: "

    try:
        # Python 2
        fp = raw_input(msg1)
        result = raw_input(msg2)
    except NameError:
        # Python 3
        fp = input(msg1)
        result = input(msg2)

    if result.startswith("y"):
        save = True
    else:
        save = False

    print(stack(fp, save=save))

从其工作目录中,通过命令行运行脚本。回答提示:

> python restack.py 

Enter path to input file e.g. ./test.txt: ./@data/test2.txt
Save results to a file? y/[n]: y
Your results have been saved to './output.txt'

您的结果应该在您的控制台中打印,并且可以选择保存到一个文件output.txt。根据您的兴趣调整任何参数。在

相关问题 更多 >

    热门问题