0

I have a csv file I wish to apply a regex replacement to with python.

So far I have the following

reader = csv.reader(open('ffrk_inventory_relics.csv', 'r'))
writer = csv.writer(open('outfile.csv','w'))
for row in reader:
    reader = re.sub(r'\+','z',reader)

Which is giving me the following error:

Script error: Traceback (most recent call last):
  File "ffrk_inventory_tracker_v1.6.py", line 22, in response
    getRelics(data['equipments'], 'ffrk_inventory_relics')
  File "ffrk_inventory_tracker_v1.6.py", line 72, in getRelics
    reader = re.sub(r'\+','z',reader)
  File "c:\users\baconcatbug\appdata\local\programs\python\python36\lib\re.py",
line 191, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

After googling to not much luck, I would like to ask the community here how to open the csv file correctly so I can use re.sub on it and then write out the altered csv file back to the same filename.

3
  • Why are you passing reader to re.sub? Pass row instead. Commented Oct 30, 2017 at 13:42
  • @MosesKoledoye row is a list and not a proper arg for re.sub either. Commented Oct 30, 2017 at 13:45
  • @schwobaseggl You're right. Brainfart. Commented Oct 30, 2017 at 13:47

2 Answers 2

1

csv.reader(open('ffrk_inventory_relics.csv', 'r')) is creating a list of lists, and when you iterate over it and pass each value to re.sub, you are passing a list, not a string. Try this:

import re
import csv
final_data = [[re.sub('\+', 'z', b) for b in i] for i in csv.reader(open('ffrk_inventory_relics.csv', 'r'))]
write = csv.writer(open('ffrk_inventory_relics.csv'))
write.writerows(final_data)
Sign up to request clarification or add additional context in comments.

1 Comment

And how do I write final_data to the csv file?
0

If you don't need csv you can use replace with regular open:

with open('ffrk_inventory_relics.csv', 'r') as reader, open('outfile.csv','w') as writer:
    for row in reader:
        writer.write(row.replace('+','z'))

5 Comments

The + and z were just test strings to replace, the final replacement is a big regex argument
Sure you can change that row into desired regex in sense row = re.sub('\+', 'z', row)
The following code is working, but how do I make outfile.csv replace ffrk_inventory_relics.csv? (No idea why the formatting is mangled I don't know how to express the below in a code block) with open('ffrk_inventory_relics.csv', 'r') as reader, open('outfile.csv','w') as writer: for row in reader: row = re.sub('(\+*,[^,]*,[^,]*,[^,]*,\ 0)', ' (Core)\\g<1>', row) writer.write(row)
Have you considered using pandas? I think it would solve your problem.#
I have no idea what pandas is or how it would help.

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.