我有一个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
如果您的
stack.txt
文件如下所示(即,它保存为.txt
文件):解决方案:
^{pr2}$要获得所需的列输出:
结果是:
我不会给你整个代码,但我会给你一些想法。在
首先,您需要读取文件的所有行:
然后,从每行中提取数据:
^{pr2}${cd1>可以生成组合。每行的打印元素组合。在
如果不关心元素的顺序,可以使用
set
获得唯一元素。在使用set
之前,应该先将列表lines
展平,可以使用itertools.chain.from_iterable
。在选项1:
.csv
,.txt
文件本机Python无法读取
.xls
文件。如果将文件转换为.csv
或.txt
,则可以使用标准库中的csv
模块:选项2:
.xls
,.xlsx
文件如果要保留原始的
.xls
格式,则必须安装third-party module到{a2}。在从命令提示符安装
^{pr2}$xlrd
:在Python中:
选项3:数据帧
您可以使用pandas数据帧处理csv和文本文件。See documentation用于其他格式。在
数据帧输出
另存为文件
注意:选项1&2的结果也可以用
pd.Series(list(items))
转换成无序的pandas列式对象。在最后:作为脚本
将上面三个选项中的任何一个保存在一个名为
restack.py
的函数(stack
)中。将此脚本保存到一个目录。在从其工作目录中,通过命令行运行脚本。回答提示:
您的结果应该在您的控制台中打印,并且可以选择保存到一个文件
output.txt
。根据您的兴趣调整任何参数。在相关问题 更多 >
编程相关推荐