Reputation: 725
By using the .replace function, I was able to open a csv file and replace some text within the string, and re save the csv file as a txt file using python. I would now like to save this new txt file (or at leas the string that is in the txt file) as a .sql file.
Code: # Read in the file with open('Region.txt', 'r') as file : filedata = file.read()
# Replace the target strings
filedata = filedata.replace("America", "Europe")
filedata= filedata.replace("South America", "East Europe")
# Write the file out again
with open('MyDebt.txt', 'w') as file:
file.write(filedata)
file.close()
Text file: (Region.txt)
select country_name from abcd.d_organizations where region = 'America' and region2 = 'South America'
union all
select 'South America' country_name from dual
order by country_name asc
Upvotes: 2
Views: 16775
Reputation: 725
I figured out what I was doing wrong.. pretty simple. Once i make the changes to the .txt file, I need to save immediately save the changes to the text (filedata)to a .sql file instead of to the same .txt file.
# Read in the file with open('Region.txt', 'r') as file : filedata = file.read()
# Replace the target strings
filedata = filedata.replace("America", "Europe")
filedata= filedata.replace("South America", "East Europe")
# Write the file out again
with open('Region.sql', 'w') as file:
file.write(filedata)
Upvotes: 2