Monday, October 27, 2025
HomeLanguagesHow to convert CSV columns to text in Python?

How to convert CSV columns to text in Python?

In this article, we are going to see how to convert CSV columns to text in Python, and we will also see how to convert all CSV column to text.

Approach:

  • Read .CSV file using pandas dataframe.
  • Convert particular column to list using list() constructor
  • Then sequentially convert each element of the list to a string and join them using a specific character or space.

For our program we are going to use the following CSV file:

Code:

Python3




# importing library
import pandas as pd
  
# Then loading csv file
df = pd.read_csv('datasets/Fruit.csv')
  
# converting ;FRUIT_NAME' column into list
a = list(df['FRUIT_NAME'])
  
# converting list into string and then joining it with space
b = ' '.join(str(e) for e in a)
  
# printing result
print(b)
  
# converting 'PRICE' column into list
d = list(df['PRICE'])
  
# another way for joining used
e = '\n'.join(map(str, d))
  
# printing result
print(e)


Output:

Apple Banana JackFruit Orange Pineapple Guava Grapes Mango

100
70
30
120
90
50
80
200

How to convert all csv column to text ?

For this, we don’t need to import any library.

Code:

Python3




# reading csv file
text = open("datasets/Fruit.csv", "r")
  
# joining with space content of text
text = ' '.join([i for i in text])  
  
# replacing ',' by space
text = text.replace(",", " ")  
  
#displaying result
print(text)


Output:

FRUIT_NAME PRICE
Apple 100
Banana 70
JackFruit 30
Orange 120
Pineapple 90
Guava 50
Grapes 80
Mango 200
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS