如何解决将多个文本文件中的列与CSV列文件python比较
我在一个文件夹中有662个文本文件,为了打开它们,我使用了在附近找到的代码:
os = '..'
path = './'
for infile in glob.glob(os.path.join(path,'*.*')):
print('current file is: ' + infile)
要吸引我使用的第一列:
with open(infile) as infile:
for line in infile:
print(line.split()[0])
但这是第一个问题,它仅提取最后一个文件的第一列,也不提取所有文件。
第二个问题,我需要将这些列与csv文件中的第一列进行比较。与pandas中的“ in.index”选项相同(但这是csv与文本)
解决方法
它打印最后一个文件或所有文件,因为在文件循环结束后打印列 它打印第一列,因为您写 line.split()[0]
您可以像这样
2.5.1 :004 > test = "This is a test"
=> "This is a test"
2.5.1 :005 > test['is'] # Check for the substring i.e. 'is' here
=> "is"
2.5.1 :006 > test['iss'] # Check for the substring i.e. 'is' here
=> nil
2.5.1 :008 > test[1] # Returns the char at given index i.e. 1
=> "h"
2.5.1 :009 > another_test = "This is a test 1"
=> "This is a test 1"
2.5.1 :010 > another_test[1] # Returns the char at given index i.e. 1
=> "h"
2.5.1 :011 > another_test['1'] # Check for the substring i.e. '1' here
=> "1"
2.5.1 :011 > another_test['2'] # Check for the substring i.e. '2' here
=> nil
我只是将所有代码放在一个循环和新循环中以打印所有列
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。