Skip to content

Instantly share code, notes, and snippets.

@swarupmtb
Last active August 9, 2024 01:26
Show Gist options
  • Save swarupmtb/ea8887460f9277ce101d699336faf0c8 to your computer and use it in GitHub Desktop.
Save swarupmtb/ea8887460f9277ce101d699336faf0c8 to your computer and use it in GitHub Desktop.
import csv
def column_to_single_line(csv_file, column_name):
column_values = []
with open(csv_file, mode='r', newline='') as file:
reader = csv.DictReader(file)
for row in reader:
column_values.append(row[column_name])
result = ', '.join(column_values)
return result
csv_file = 'tmalef.csv'
column_name = 'Mobile'
result = column_to_single_line(csv_file, column_name)
print(result)
# Define the file path
file_path = 'input.txt'
# Read the content of the file
with open(file_path, 'r') as file:
input_text = file.read()
# Convert the multi-line text to a single line separated by commas
output_text = ', '.join(input_text.splitlines())
# Print the result
print(output_text)
# Optionally, you can save the output to another file
output_file_path = 'output.txt'
with open(output_file_path, 'w') as output_file:
output_file.write(output_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment