Python creates a dictionary and replaces them with another file

I have two delimited files. From one.csv, I created a dictionary that looks like this:

'EB2430': ' "\t"idnD "\t"yjgV "\t"b4267 "\n',
'EB3128': ' "\t"yagE "\t\t"b0268 "\n',
'EB3945': ' "\t"maeB "\t"ypfF "\t"b2463 "\n',
'EB3944': ' "\t"eutS "\t"ypfE "\t"b2462 "\n',

I would like to insert the value of the dictionary into the second.csv file, which looks like this:

"EB2430"    36.81   364 222 4   72  430 101 461 1.00E-063   237
"EB3128"    26.04   169 108 6   42  206 17  172 6.00E-006   45.8
"EB3945"    20.6    233 162 6   106 333 33  247 6.00E-005   42.4
"EB3944"    19.07   367 284 6   1   355 1   366 2.00E-023   103 

With the resulting output tab:

'EB2430'   idnD   yjgV   b4267   36.81   364 222 4   72  430 101 461 1.00E-063   237
'EB3128'   yagE   b0268   26.04   169 108 6   42  206 17  172 6.00E-006   45.8
'EB3945'   maeB   ypfF   b2463   20.6    233 162 6   106 333 33  247 6.00E-005   42.4
'EB3944'   eutS   ypfE   b2462   19.07   367 284 6   1   355 1   366 2.00E-023   103

Here is my code for creating a dictionary:

f = open ("one.csv", "r")
g = open ("second.csv", "r")
eb = []
desc = []
di = {} 

for line in f:
    for row in f:
        eb.append(row[1:7])
        desc.append(row[7:])

di = dict(zip(eb,desc))

Sorry for being so long! I have not programmed for a long time.

Hooray!

Sat

+3
source share
3 answers

It looks like you could more conveniently use the Python csv standard library here. rather than performing parts of the word processing yourself "manually." For instance:.

import csv
with open("one.csv", "r") as f:
  rows_one = list(csv.reader(f, delimiter='\t'))
with open("second.csv", "r") as g:
  rows_two = list(csv.reader(g, delimiter='\t'))
rows_totl = [r + s[1:] for r, s in zip(rows_one, rows_two)]
with open("total.csv", "w") as h:
  csv.writer(h, delimiter='\t').writerows(rows_totl)

with Python 2.6 ( 2.5, from __future__ import with_statement! -) - , with ... , , .

+2

CSV csv, . , .. API :

import csv

# Auto-detector of this particular CSV dialect (delimiters and such)
dialect = csv.Sniffer().sniff(open('one.csv').read())

# csv.reader yields every row found in the file using the given dialect
rows = csv.reader(open('one.csv'), dialect = dialect)

# [list comprehension][2]
resulting_dict = dict((row[0], row[1:]) for row in rows)

( , , ).

dicts , dict1 dict2, :

combined_dict = dict((key, dict1[key] + dict2[key]) for key in dict2)

.csv :

writer = csv.writer(open('second.csv', 'w'), delimiter = '\t')
for key, values in combined_dict:
    writer.writerow(key, *values)

docs .

: (dict ). :

  • Python 3 Python 2.7, .OrderedDict
  • - , for .
0

Check out the csv module:

import csv
reader1 = csv.reader(open('input1.csv'), delimiter = '\t')
reader2 = csv.reader(open('input2.csv'), delimiter = '\t')
csvwriter = csv.writer(open('output.csv', 'w'),delimiter = '\t')
while True:
    row1 = reader1.next()
    if row1:
       row2 = reader2.next()
       new_row = row2 + row1[1:]
       csvwriter.writerow(new_row)
    else:
        break
0
source

Source: https://habr.com/ru/post/1739517/


All Articles