0

I have a text file with a punch of keywords: baseball,player,bat,creek,home,guy,squatting,plane, something like this and I want a code to change them to be like this: 'baseball','player','bat','creek','home','guy','squatting','plane'.

Code:

import pandas as pd 
import csv 
file = pd.read_csv('/home/greshad/code/data_vis/error_score-updated.csv') 
out = open('/home/greshad/code/data_vis/keywords.txt', 'w') 
for i in range(len(file)): 
    out.writelines(file['words'][i] + ',')

Drees
  • 688
  • 1
  • 6
  • 21
Ghazal
  • 42
  • 8
  • 1
    ...and what have you tried so far? – norok2 Aug 01 '19 at 21:25
  • Possible duplicate of [How to search and replace text in a file?](https://stackoverflow.com/questions/17140886/how-to-search-and-replace-text-in-a-file) – Xukrao Aug 01 '19 at 21:26
  • 1
    please provide a [MCVE] – Nathan McCoy Aug 01 '19 at 21:26
  • import pandas as pd import csv file = pd.read_csv('/home/greshad/code/data_vis/error_score-updated.csv') out = open('/home/greshad/code/data_vis/keywords.txt', 'w') for i in range(len(file)): out.writelines(file['words'][i] + ','). Here is my code so far – Ghazal Aug 01 '19 at 21:30
  • @roganjosh I think he does want to add the quotation marks. – Drees Aug 01 '19 at 21:36
  • Found the solution: import pandas as pd import csv file = pd.read_csv('/home/greshad/code/data_vis/error_score-updated.csv') out = open('/home/greshad/code/data_vis/keywords.txt', 'w') for i in range(len(file)): out.writelines("'%s'" %file['words'][i] + ',') – Ghazal Aug 01 '19 at 22:04

3 Answers3

0

This is only for comma delimited csvs.

This is a janky solution but it solves your problem: (I do this just by reading the raw file - it'll only do one line but it literally would be a 20 second change... the only reason I haven't changed that was because you didn't provide a good example of what you wanted to do) I'm sure you could find the 'proper' answer with one minute of research.

file = open('error_score-updated.csv', 'r+') 
out = open('keywords.txt', 'w+') 
output_string = ""
for cells in file:
    for word in cells.split(','):
        output_string += ("'{}', ".format(word))

out.write(output_string[:-2])
Ben Rauzi
  • 564
  • 4
  • 13
  • Thank you ... I have tried a bunch things but didn't work. but thank you again. What I need is to change the format of word cat to 'cat', . – Ghazal Aug 01 '19 at 21:56
  • That will do that for the top row. If it's already outputted into a text file this should solve your problem as well. Can you give an example of the exact file you want to change from and to? – Ben Rauzi Aug 01 '19 at 22:17
0

Here is what I have found that works:

file = pd.read_csv('/home/greshad/code/data_vis/error_score-updated.csv') out = open('/home/greshad/code/data_vis/keywords.txt', 'w')

for i in range(len(file)): out.writelines("'%s'" %file['words'][i] + ',')

Ghazal
  • 42
  • 8
0

Or just for fun (only for fun / vintage):

def MrKeys(keyList):
  val = []
  for x in keyList: val.append(x) if x!=',' else val.append(' ')
  val_srt = str(val)
  return val_srt.replace(", ","").replace("'' ''",",").replace("''","").replace(",","','")

And call it, ex: >>> print (MrKeys(words))

Or if the txt file is all likes "baseball,player,bat,creek,home,guy,squatting,plane" try with:

print (('\'')+word_list.replace(",","','")+('\''))

aliquis
  • 1
  • 2