r/pythonhelp • u/WizardOfOdz_777 • May 17 '22
Pull data from another table if contains ...
Hello - I have 2 csv files each containing a data . I would like python to read in the files, then if any value from file 1 is found in file2, it will add that value to file2 in a new column. When completed, it will save new file file3 as csv.
file1
| NAME |
|---|
| word1 |
| word2 |
| word3 |
| word4 |
| word5 |
| word6 |
file2
| SN | NAME |
|---|---|
| 1083 | word1 and other text |
| 1099 | word6 with more text |
| 1146 | text with word2 |
| 1171 | more with word2 |
| 1329 | word4 |
| 1446 | word5 blah blah |
| 1662 | here is word3 |
| 1777 | word3 again |
file3
| SN | NAME | FOUND |
|---|---|---|
| 1083 | word1 and other text | word1 |
| 1099 | word6 with more text | word6 |
| 1146 | text with word2 | word2 |
| 1171 | more with word2 | word2 |
| 1329 | word4 | word4 |
| 1446 | word5 blah blah | word5 |
| 1662 | here is word3 | word3 |
| 1777 | word3 again | word3 |
2
Upvotes
1
u/WizardOfOdz_777 May 17 '22 edited May 17 '22
The code that I have written for the actual project (below) is missing what I am trying to do:
- It only looks at the line, if it is in there, it prints the line. It does not add to another column
- I must make sure everything is capitalized in the first file, because all the data is capitalized in the 2nd file
- it does not save to a file.
brand = []
with open (r'C:\python_files\ez1.csv', 'r') as f:
# skip first line
f.readline()
for line in f:
brand.append(line.strip("\n"))
with open (r'C:\python_files\ez2.csv', 'r') as f:
# skip the first line
f.readline()
for line in f:
if (line.split(",")[2]) in brand:
print (line)