简体   繁体   中英

read csv file as a string in python

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.

它应该与以下代码一起使用,但这不是处理csv的最佳方法。

csv_string = ''.join(open(filepath, 'r').readlines())

Something like:

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

You can read csv from this .

import csv

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM