![](/img/trans.png)
[英]How to read a csv row wise and convert to a dataframe column using pandas?
[英]How to read through a text file in row and column wise manner
我有一個文本文件,其數據分為3列,例如
1 1 -1
1 -1 -1
-1 1 1
1 1 1
-1 1 1
現在,我要檢查該行是否至少包含一個“ 1”,如果是,則在與該行相對應的輸出文件中打印“ 1”。 如果該行中的任何列都不包含“ 1”,則在與該行相對應的輸出文件中打印“ -1”。
我不知道如何使用row * column明智的方式處理文本文件。 你能幫我處理一下嗎?
該程序遍歷輸入文件的行,每行發出“ 1”或“ -1”。
for row in input_file
遍歷所有row
,依次將每個行分配給變量row
。
row.split()
創建一個字符串數組,其中每個字符串代表該行中的一列。
if '1' in row.split()
為true,則row.split()
返回的至少一項完全為'1'
。
with open('input.txt') as input_file:
for row in input_file:
if '1' in row.split():
print 1
else:
print -1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.