简体   繁体   中英

Comparison on two columns from two text files using numpy

I have two files with the txt format as shown below:

File 1: Delimiter ^

ID^Name^Address^Date of Join^Pin^Country
1^John^No 123. Ml^2016-06-30^123123^USA
2^Shaw^No 13. MX^2014-02-30^423123^UK
3^Donn^No 453. MR^2015-01-30^523123^UAE

File 2: Delimiter |

Name|ID|Address|Date of Join|Pin|Country
John|1|No 123. Ml|2016-06-30|123123|USA
Shaw|2|No 13. MX|2014-02-30|423123|UK
Donn|3|No 453. MR|2015-01-02|523123|UAE
Donn|4|No 1. XR|2012-03-10|523123|UAE

I want to do comparison on two 2 columns namely ID and Date of Join .

My try : I am using numpy for this.

import numpy as np
data1 = np.loadtxt('E:\File1.txt', delimiter='^')
data2 = np.loadtxt('E:\File2.txt', delimiter='|')
np.savetxt('E:\Output.txt',data2[data1[:,0]!=data2[:,1] or data1[:,3]!=data2[:,3],:])

But getting an error:

ValueError: could not convert string to float: ID

使用np.genfromtxt dtype=None np.genfromtxt进行自动数据类型检测:

data1 = np.loadtxt('E:\File1.txt', dtype=None, delimiter='^')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM