0

I accidentally corrupted a csv file (delimiters no longer working - thanks Microsoft Excel!). I want to salvage some data by reading it as a string and searching for things - I can see the text by opening the file on notepad, but I can't figure out how to load that string from the filepath in python.

I imagine it would be a variation of

csv_string = open(filepath, 'something').read()

but I can't get it to work, or find a solution on SO / google.

2
  • 1
    A sample of your corrupted file, perhaps? Commented Apr 16, 2019 at 10:20
  • Sorry, the file is huge with very little relevant information - it's just a csv file with delimiters removed. I'm still not sure how I would make a useful sample. Commented Apr 16, 2019 at 10:38

3 Answers 3

1

It should work with the following code, but it is not the best way to deal with csv.

csv_string = ''.join(open(filepath, 'r').readlines())
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly! I know it's not the best, this is just to recover a bit of work.
0

Something like:

with open(filepath, 'r') as corrupted_file:
    for line in corrupted_file:
        print(line) # Or whatever

Comments

0

You can read csv from this .

import csv

reader = csv.reader(open("samples/sample.csv"))    
for title, year, director in reader:
   print year, title

1 Comment

OP did not mention anything about title, year, director

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.