Last active
August 9, 2024 01:26
-
-
Save swarupmtb/ea8887460f9277ce101d699336faf0c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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) |
This file contains hidden or 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
# 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