Reputation: 13
I created a code to take two .txt files, compare them and export the results to another .txt file. Below is my code (sorry about the mess).
Any ideas? Or am I just an imbecile?
Using python 3.5.2:
# Barcodes Search (V3actual)
# Import the text files, putting them into arrays/lists
with open('Barcodes1000', 'r') as f:
barcodes = {line.strip() for line in f}
with open('EANstaging1000', 'r') as f:
EAN_staging = {line.strip() for line in f}
##diff = barcodes ^ EAN_staging
##print (diff)
in_barcodes_but_not_in_EAN_staging = barcodes.difference(EAN_staging)
print (in_barcodes_but_not_in_EAN_staging)
# Exporting in_barcodes_but_not_in_EAN_staging to a .txt file
with open("BarcodesSearch29_06_16", "wt") as BarcodesSearch29_06_16: # Create .txt file
BarcodesSearch29_06_16.write(in_barcodes_but_not_in_EAN_staging) # Write results to the .txt file
Upvotes: 1
Views: 281
Reputation: 154
From the comments to your question, it sounds like your issue is that you want to save your list of strings as a file. File.write
expects a single string as input, while File.writelines
expects a list of strings, which is what your data appears to be.
with open("BarcodesSearch29_06_16", "wt") as BarcodesSearch29_06_16:
BarcodesSearch29_06_16.writelines(in_barcodes_but_not_in_EAN_staging)
That will iterate through your list in_barcodes_but_not_in_EAN_staging
, and write each element as a separate line in the file BarcodesSearch29_06_16
.
Upvotes: 2
Reputation: 422
Try BarcodesSearch29_06_16.write(str(in_barcodes_but_not_in_EAN_staging))
. Also, you'll want to close the file after you're done writing to it with BarcodesSearch29_06_16.close()
.
Upvotes: 1