0

I want to write a .csv file. One of the columns is "words". Each category of words is in a row, and the cell "words" has a list of words that I read as:

words = []

for i in range(len(category)):

    r = requests.post(base_url+'/'+url[i])

    if r.ok:
        data = r.content.decode('utf8')
        words.append(pd.Series.tolist((pd.read_csv(io.StringIO(data), squeeze=True)).T))

url_f = [base_url + s  for s in url]


df = pd.DataFrame({'category': category, 'url': url_f, 
                   'words': words})

df.to_csv("lm_words.csv")

the list of words is downloaded as r.

The table looks something like this:

index | category | url | words
0.    | cat1.    | www.| [word1, word2, word3]

And I am trying to get rid of the brackets in [ word1, word2, word3 ].

I have this written in R and it doesn't print the brackets in the .csv

Edit1: Format

DCN
  • 137
  • 1
  • 11

2 Answers2

1

Use str.join

Ex:

df = pd.DataFrame({'category': category, 'url': url_f, 
                   'words': words})
df["words"] = df["words"].apply(", ".join)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

If you want to get rid of square brackets then first you need to convert the list into string using

    new_string = str(list_name)

After that you can remove the square brackets using slicing technique in python:

    sliced = new_string[1:-2]
manishm
  • 71
  • 8
  • I added: new_string = str(words) sliced = new_string[1:-2] But I still got the brackets. – DCN May 13 '19 at 10:09