だから私は、ARを試してみましたこれは面白いと思っていて、私は現在Pythonを学んでいるので、ちょっとおびただしいです。
次のコードを見つけてください。
これはサンプルで機能します。 しかし、マスターファイルに次のキーの穴がある場合は、順番を混ぜます。私はそれを修正することができませんでした。
重複したプライマリキーを複数の行に分散させたデータ構造には、多くの問題がありました。
あなたは正確に何をしているのかわかりませんが、私はデータベースで多くの作業をしており、この種のデータ構造は非常に珍しいことを伝えています。データセットを再構築する場合は、おそらく多くのメリットがあります。
この量のデータを使用すると、おそらくデータベースに格納することで利益を得ることができます。あなたがそれ以上の深い学習の藻を実行していない場合。
例: これは、それは順序をアップミックスの例ですが、nontheless
マスターファイル
Key1|AAA|BBB|CCC
Key1|AAA|BBB|DDD
Key1|XXX|YYY|ZZZ
Key2|ZZZ|YYY|123
Key2|EEE|FFF|RRR
Key3|RRR|EEE|GGG
Key3|SSS|TTT|GGG
Key7|RRR|EEE|GGG
Key7|SSS|TTT|GGG
changefile
Key1|111|222|333
Key1|222|333|444
Key1|222|333|555
Key4|888|333|222
Key4|888|777|222
Key5|888|333|222
Key5|888|777|222
Key6|888|333|222
Key6|888|777|222
Key8|888|333|222
Key8|888|777|222
Key9|888|333|222
Key9|888|777|222
コード作品:
を
import fileinput
with open('changefile.txt') as infile:
keyindex = []
for line in infile:
linelist = line.strip().split("|") ## split line by |
key = linelist[0] ## assign the key
keyid = linelist[0][3:] ## assign keyid
keylist = [] ## assign keylist for loop
## finding duplicate keys in changefile and assign them to list
if key not in keyindex: ## we need this because multiple keys in multiple lines
with open('changefile.txt') as infile2:
#spawning extra loop for each new key to open and search all duplicate keys and assign them to list
for line2 in infile2:
if line2.startswith(key):
print(line2)
keylist.append(line2)
## Delete line with current key of loop from master file
keyindex.append(key)
print(keylist)
for linem in fileinput.input('test.txt', inplace=True):
if key in linem:
continue
print(linem, end='')
## insert keys from keyindex
for linei in fileinput.input('test.txt', inplace=1):
if 'Key'+str(int(keyid)+1) in linei: ## This statement is case sensitive
for item in keylist:
print(item, end='')
keylist = []
print(linei, end='')
# I had problems with not beeing able to go to next line at the beginning of this code if you fix this, this would be better then opening the file anew
## if last in linei and keylist:
## ##print('\n')
## for item in keylist:
## print(item, end='')
## keylist = []
## print('\n')
## this block may cause problem with memory you may can fix this with the comment block before this.
## this block is for adding left over keys from the end of change file to the end of master e.g. id 9 is in changefile, but masterfile is only going to key8
with open("test.txt", "a") as myfile:
if keylist:
for item in keylist:
myfile.write(item)
keylist = []
else:
continue
## because we spawned a seperate loop each time we find a new key, we can skip the duplicate lines
else:
## print('>>>key '+line+'already worked at! go to next line') # if you want to skip, uncomment continue and comment this
continue
##print all keyindexes that have been changed
print('Following keys have been changed:'keyindex)
スペーサーだけがありますか?あなたのファイルに行デリミタはありませんか? "key1"の部分がプライマリキーである場合、なぜ "key1"が複数あるのでしょうか?どのkey1をどのkey1と交換するかをどのように決定するのですか?変更ファイルにはkey1の2倍しかないが、マスターファイルには3x – veritaS
があります。それは整列の問題があったように見え、私は私の質問で同じを修正しました。一致するキー値は両方のファイルに重複しています。マスターファイルからすべてのレコードを削除し、一致するキーのレコードを変更ファイルから保持する必要があります。たとえば、Key1は両方のファイルに共通です。マスターファイルにkey1のレコードが3つ、変更ファイルに2つのレコードがあります。出力ファイルには変更ファイルのレコードが2つしかなく、マスターファイルの3つのレコードはすべて無視されます。出力。 – pythonlearner1985
私はあなたのコンピュータをオフにし、紙と鉛筆を取得することをお勧めします。言葉で**を記述する**この問題を解決するために必要なステップ。 Python構文については心配しないでください。ソリューションがどうあるべきかを明確に理解してください。 ** **あなたがそれをした後、あなたのコンピュータを再びオンにし、あなたのステップをPythonコードに翻訳しようとします。 –