I'd like to ask you how to load a file with Python replace.

Asked 2 years ago, Updated 2 years ago, 101 views

Sorry for the basic question.

In the Python text cleaning process, change the contents one by one as shown below

df['sentence'] = df["sentence"].str.replace ("ballpoint pen", "pen")
df['sentence'] = df["sentence"].str.replace ("sign pen", "pen")
df['sentence'] = df["sentence"].str.replace ("Magic", "Pen")

I'd like to load the file and do it like processing the disused words as below.

stopwords = []
f = open('/content/khaiii/rsc/src/incompatible.txt')
lines = f.readlines()
for line in lines:
    line = line.strip()
    stopwords.append(line)
f.close()

df['sentence'] = df['sentence'].apply(lambda x : [item for item in x if item not in stopwords])

Is there a way?

python replace

2022-09-20 18:56

2 Answers

Replacement list = []

# Read the file, and create a replacement list.
with open (word list file pass, "r") as f:
  for line in f:
    Before, after, = line.split('\t')
    Replacement list.append(before changing, after changing)

# Call up a pair of words in the list, and perform a replacement.
For before and after molding In Replacement List:
  df["Content"] = df["Content"].str.replace (before molding, after molding)

This is how you do it. When reading a file, check the encoding of the file carefully. If there's a useless line change at the end of the file, you'll have to take care of it.


2022-09-20 18:56

I don't know if you understand me well because I'm short-sighted.

I think I can do it like this, so I'm leaving it

with open('/content/khaiiii/rsc/src/disambiguation)txt', 'r') as f:
    lines =  f.readlines()
    stopwords = [line.strip() for line in lines]

for fd in df['sentence']:
    if fd in stopwords:
        break
    for in ['ball pen', 'sign pen', 'magic']:
        fd = fd.replace (r, 'pen')
Or
fd = str(df['sentence'])
if fd not in stopword:
    for in ['ball pen', 'sign pen', 'magic']:
        fd = fd.replace(r, 'pen')


2022-09-20 18:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.