[英]read csv file as a string in python
我不小心損壞了一個csv文件(分隔符不再起作用-感謝Microsoft Excel!)。 我想通過將其讀取為字符串並進行搜索來挽救一些數據-我可以通過在記事本上打開文件來查看文本,但是我不知道如何從python的文件路徑中加載該字符串。
我想這將是
csv_string = open(filepath, 'something').read()
但我無法使其正常運行,或者無法在SO / google上找到解決方案。
它應該與以下代碼一起使用,但這不是處理csv的最佳方法。
csv_string = ''.join(open(filepath, 'r').readlines())
就像是:
with open(filepath, 'r') as corrupted_file:
for line in corrupted_file:
print(line) # Or whatever
您可以從this讀取csv。
import csv
reader = csv.reader(open("samples/sample.csv"))
for title, year, director in reader:
print year, title
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.