1

I try to replace a text in csv read with some data dictionary, but I got an Error.

import csv

dataset = open('../sentimenprabowo.csv', 'r')
sentiment = csv.reader(dataset, delimiter=',')

newDok = open('../sentimenprabowopreproses.csv', 'w')
save = csv.writer(newDok)

data= open("convertcsv.json", "r")
APPOSTOPHES=data.read()

new_sentence = []
for row in sentiment:     
    print(row)
    for candidate_replacement in APPOSTOPHES:                
        if candidate_replacement in row:            
            #print(candidate_replacement)
            row = row.replace(candidate_replacement, APPOSTOPHES[candidate_replacement])
    new_sentence.append(row)
rfrm = "".join(new_sentence)
print(rfrm)

I hope this can be replace all the text in my csv who same text with data dictionary (correction spelling).

but the result is error:

  File "readdata.py", line 45, in <module>
    row = row.replace(candidate_replacement, APPOSTOPHES[candidate_replacement])
AttributeError: 'list' object has no attribute 'replace'

help me please...

this is my convertcsv.json file:

{"@":"di","ababil":"abg labil","abis":"habis","acc":"accord","ad":"ada","adlah":"adalah"}
4
  • 1
    replace can only apply to str, seems row is list here. Commented Apr 10, 2019 at 6:07
  • so, how to fix it? Commented Apr 10, 2019 at 6:10
  • To replace list items check stackoverflow.com/questions/2582138/… Commented Apr 10, 2019 at 6:11
  • I think you should figure out the composition of your data, I can not debug the context, maybe for s in row? Commented Apr 10, 2019 at 6:13

2 Answers 2

3

As the error says your row is a list and doesn't have replace method. You can convert it to string replace as you wish then again cast to array:

row = str(row).replace(candidate_replacement, APPOSTOPHES[candidate_replacement]).split(',')
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this with list comparison

use this

for r in range(len(row)):
    if row[r] == candidate_replacement:
        row[r] = APPOSTOPHES[candidate_replacement]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.