Created
July 10, 2023 14:15
-
-
Save hassanvfx/b445fbdcabe29957670aff4cd34cc9dd to your computer and use it in GitHub Desktop.
This script extract only the text from the twitter scraper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sys | |
import os | |
import re | |
def remove_urls(text): | |
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') | |
return url_pattern.sub(r'', text) | |
def read_csv(filename, output_filename): | |
with open(filename, 'r') as csvfile: | |
reader = csv.DictReader(csvfile) | |
with open(output_filename, 'w') as txtfile: | |
for row in reader: | |
content = row['Content'] | |
if content is None: | |
content = '' | |
else: | |
content = remove_urls(content) | |
txtfile.write(content + '\n') | |
def main(): | |
if len(sys.argv) != 2: | |
print(f'Usage: {sys.argv[0]} input_filename') | |
sys.exit(1) | |
input_filename = sys.argv[1] | |
basename = os.path.splitext(input_filename)[0] | |
output_filename = basename + '.txt' | |
read_csv(input_filename, output_filename) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run this like
python output_text.py twitter_account.csv