Вопрос

I want to

  • Open and read 1st file
  • Open and read 2nd file
  • Copy the values of 2nd file to 1st file with header
  • Write new value to 1st file

That is, 1st file is opened as read-write mode and 2nd file is as read mode. For instance,

1st_file

     CHINESE    JAPANESE   KOREAN
CA   0.1        0.1        1.1
WA   0.2        -0.2       1.3
OR   -0.1       1.1        0.1
UT   0.3        1.4        -0.9

2nd_file (no header)

1.1
1.3
-0.1
1.3

recreated 1st_file

     CHINESE    JAPANESE   KOREAN    VIETNAMESE   TOTAL
CA   0.1        0.1        1.1       1.1          2.4
WA   0.2        -0.2       1.3       1.3          2.6
OR   -0.1       1.1        0.1      -0.1          1.0
UT   0.3        1.4        -0.9      1.3          2.1

Here, 2nd_file contains the values about VIETNAMESE column.

So, first thing is to write the header, 1) VIETNAMESE and 2) TOTAL to the header of 1st_file.

Then, write the value from 2nd_file to the corresponded VIETNAMESE column of 1st_column.

Finally, calculate the values of 1st_column and write it (such as TOTAL) to the 1st_column.

I've tried to open 1st file with r+ mode but it did not well work. FYI, real 1st_files has about 100 millions of rows and twenty columns.

How to do it?

Это было полезно?

Решение

Although i agreed with iCodez and you shouldn't use txt files (maybe SQL or even json)... i'll give you an alternative.

file1 = open("example.txt", "r")
alldatainfile1 = file1.read()
file1.close()

file2 = open("example.txt", "r")
alldatainfile2 = file2.read()
file2.close()

Now that you are working with vars and not files you could...

file1 = open("example.txt", "w")
file1.write(alldatainfile2)
file1.close()

Notice that i use "w" to write in the file (wich will delete all info and then save new one), but if you just want to add info to the file instead of deleting all you should use "a" for appending data.

Finally i suggest 3 tips:

  • Backup your files before trying, the chances to delete important info is HIGH.
  • Use a For line in yourfile code to check if the info is already there, and don't duplicate it if that's the case, but that should be done properly with json.
  • if that was json would be easy, since is not i'd try to give you a code to calculate the total of a row.

You could do this code:

total = 0
for line in alldatainfile1:
  linesplit.split("   ") #3 whitespaces, since you got it that way
  total = total + line[1]
print("total of column1: " + str(total))

Другие советы

I prefer to use readlines() for editing text files. This should do the trick:

fileA = open("whatever the file name of first file is", 'r')
fileALines = fileA.readlines()
fileA.close()

fileB = open("whatever the file name of second file is", 'r')
fileBLines = fileB.readlines()
fileB.close()

newLines []

newLines[0] = fileALines[0] "VIETNAMESE  TOTAL"  #I'm not sure how you intend on getting the column header, but you can just insert it here.

lengthList = [len(header) for header in fileALines[0]] #Used for column widths

for lineA,lineB in zip(fileALines[1:],fileBLines):
    itemList = (lineA + lineB).split()
    itemList.append(str(sum(map(float,itemList))))
    for length,item in zip(lenghtList,itemList):
        newLines.append("{:^{length}}".format(item, length=length))
    newLines.append("\n")

fileC = open("newFile.txt", 'w')
for line in newLines:
    fileC.write(line)
fileC.close()

Using the code as I've written it will create a third file which you can use to debug it if you have any issues.

THIS CODE WILL NOT WORK IF:

  • You have a different number of rows in your two files (excluding the header row)
  • You have a number which is wider than the header
  • Your sum column ends up being wider than the header
  • I've made some sort of silly error

I also agree with the comments and other answers, text files are probably not the best way to do it, but it can be done. Hope this helps.

You can try the following code :

FILE_1 = "File1.in"
FILE_2 = "File2.in"


def getTableData(file_name):
    """Retreive the Table Data from 'file_name' and return it as a list()"""
    file_1 = open(file_name,'r')
    data =  [cols.split() for cols in file_1.read().split('\n')]
    data[0].insert(0,' ')
    return data

def getColumn(file_name):
    """Retrieve the new Column data from file 'file_name' and return it as a list"""
    file_2 = open("File2.in", 'r')  
    col = file_2.read().split('\n')
    return col

def appendColumn(table, col_name, col):
    """Append the new Column to the table"""
    table[0].append(col_name)
    for x in xrange(len(col)):
        table[x+1].append(col[x])
    return table

def total(table):
    """Calculate the Total in the table"""
    col =[]
    for i in xrange(len(table)-1):
        tot = 0.0
        for j in xrange(len(table[i+1])-1):
            tot += float(table[i+1][j+1])
        col.append(str(tot))
    return col

def writeBack(file_name, table):
    """Writing the table back to 'file_name'"""
    fout = open(file_name,"w")
    for row in table:
        line = '\t\t'.join(row)
        fout.write(line + "\n")


table = appendColumn(getTableData(FILE_1), "VIETNAMESE", getColumn(FILE_2))
col = total(table)
table = appendColumn(table, "TOTAL", col)
writeBack(FILE_1, table)

Limitations:

  • The columns that will be printed in the final Output file will not be propely Indented. You will have to play around with the indentation. Currently each column is separated by two '\t'.
  • The code will only work if the new column being added has the same number of rows as the existing table.
  • As Saelyth already mentioned, the "w" option will delete the previous file and create a new one. So make sure you back up your data before trying this out.

I am also assuming that the new column name is not included in the second file, and that it is received from a different source.

The final data table that you are writing back is a 2-Dimensional matrix, so you can edit any entry at (i,j) by simply doing table[i][j] = "New Data".

If you want quick and structured files use python's csv library.

import csv
main_headers = ['state', 'chinese']
compound_data = []
with open('languages1.csv', 'r') as csv_file:
    csvreader = csv.DictReader(csv_file)
    for row in csvreader:
        compound_data.append(row)
print(compound_data)
with open('languages2.csv', 'r') as csv_file:
    csvreader = csv.DictReader(csv_file)
    for row in csvreader:
    compound_data.append(row)
print(compound_data)

Output:

[{'state': 'ca', 'chinese': '1.0'}, {'state': 'vt', 'chinese': '2.0'}]
[{'state': 'ca', 'chinese': '1.0'}, {'state': 'vt', 'chinese': '2.0'}, {'state': 'ca', 'vietnamese': '-0.1'}, {'state': 'vt', 'vietnamese': '1.5'}]

Once you have your data you can rewrite to csv file or whatever file you want and apply formatting.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top