[英]Validate first 3 rows of txt file (tsv) in Python
我一直在嘗試為上傳到我的環境的 txt 文件構建驗證規則。 這些文件是制表符分隔的,我需要驗證格式如下的前 3 行:
## This Text Here
## This Text Here
## This Text Here
我需要建立一個通過失敗驗證。 我已經嘗試使用 python 中的內置 csv function 執行此操作,但到目前為止沒有運氣。 希望獲得有關通往 go 的最佳路線的任何建議。
嘗試這個:
### it depends on how you open the file but...
# open using with..
with open("test.tsv") as inData:
# split lines on tabs...
allLines = [l.split("\t") for l in inData]
# get the lines in question:
testLines = [l[0] for l in allLines[:3]]
# then you could use assert
for l in testLines:
assert(l.startswith("##"))
# and whatever other validation you need for the string
### you could ad try/except
try:
for l in testLines:
assert(l.startswith("##"))
except AssertionError as e:
print(e, "please use a validated file!")
進一步閱讀: https://www.tutorialspoint.com/python/python_exceptions.htm
也許您應該嘗試一下 pandas:
import pandas as pd
file_name = # your file name
csv = pd.read_csv(file_name, sep='\t')
# do your stuff
文檔: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.