0
Pythonで複数のファイルを読み込み、それらの間のマッピングを行いたいとします。Pythonでファイルを読み込み、外出先で特定の文字列を置き換えます。
私はこれらのことでかなり新しいので、他の人からコードを入手しました。しかし、今私はそれを編集したい。そして、私は完全にPythonのマクロを理解することはできません。
だからここにコード
def getDataFromFile(infile):
'''
Opens a file, processes it by replacing all the \t\t
with \t'n/a'\t and returns to the user the header of the file,
and a list of genes.
'''
with open(infile, 'r') as f:
reader = csv.reader(f, delimiter='\t') # Open the file with csv.reader so it has a cleaner look to it.
header = f.readline() # Store header on a variable
list = [[x if x else 'n/a' for x in line] for line in reader] # This is done, so we can have 1 universal input. n/a is for non-existent value!
# Most databases, don't insert a special character for non-existent
# values, they just \t\t it! So be careful with that!
# With the above approach, we end up with a list of lists
# Every column, will have a value and that will be either the one provided by the file
# or, the "our" special for non-existent attributes, 'NaN'
header = header.split() # header should be a list of strings.
return header, geneList
、それは'/t/t'
をチェックし'n/a'
に置き換えるだけでなく、'NA'
のような「非存在」の他の形態を探していないだけように、どのように私は、この行list = [[x if x else 'n/a' for x in line] for line in reader]
を変更することができますがあります(Rで使用される)。
私はそれがnoobの質問ですが、私は2週間前にPythonを使い始めました。私はまだ学習中です。
これは、以前のものよりはるかに明確です。ありがとう:)これはコピー貼り付けの問題です。何が起こっているのか理解できません。あなたはそれを行うと、悪い学習の練習である前方に移動 – Pavlos
コピー/貼り付けは、コードが良い限り限りOKです:)リストの理解は理解するためにしばらくする必要があります。その後、それは本当に強力です。 –