如何',,,,,,,,,,,,,,,,,,\n'使用 python 的 ReadLine() 在 CSV 檔案中找到一個看起來像這樣的空行。
我想寫這樣的東西:
file = 'file.csv'
f = open(file, mode='r', encoding='utf8')
while f.readline() != '\n' or f.readline() != ',,,,,,,,,,,,,,,,,,\n':
pass
df = pd.read_csv(f, header=None)
f.close()
or f.readline() != ',,,,,,,,,,,,,,,,,,\n':沒有識別看起來像這樣的行',,,,,,,,,,,,,,,,,,\n'
我覺得f.readline()在我的筆記本中一遍又一遍地手動運行時會起作用,直到我到達第一個“空白”行。然后輸出看起來像',,,,,,,,,,,,,,,,,,\n',我認為肯定會撿起它。不用說它沒有。
uj5u.com熱心網友回復:
通過在該陳述句中呼叫f.readline()兩次or,您實際上正在處理兩行。在評估之前嘗試將其存盤為變數,這樣您一次只處理一行。這是否滿足您的需求?:
import pandas as pd
file = 'file.csv'
f = open(file, mode='r', encoding='utf8')
if_continue = True
while if_continue:
current_line = f.readline()
print(f"current_line is {current_line}")
if current_line != '\n' or current_line != ',,,,,,,,,,,,,,,,,,':
if_continue = False
print("Exiting while loop")
df = pd.read_csv(f, header=None)
f.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465304.html
上一篇:讀取帶有空行的csv檔案
