![]() 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 = "New Data". 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. So make sure you back up your data before trying this out. As Saelyth already mentioned, the "w" option will delete the previous file and create a new one.The code will only work if the new column being added has the same number of rows as the existing table.Currently each column is separated by two '\t'. You will have to play around with the indentation. The columns that will be printed in the final Output file will not be propely Indented.Table = appendColumn(table, "TOTAL", col) Table = appendColumn(getTableData(FILE_1), "VIETNAMESE", getColumn(FILE_2)) """Writing the table back to 'file_name'""" """Retrieve the new Column data from file 'file_name' and return it as a list""" """Retreive the Table Data from 'file_name' and return it as a list()"""ĭata = You can try the following code : FILE_1 = "File1.in" 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. The canonical way to create a file object is by using the open () function. Your sum column ends up being wider than the header Python File I/O - Read and Write Files In Python, the IO module provides methods of three types of IO operations raw binary files, buffered binary files, and text files.You have a number which is wider than the header.You have a different number of rows in your two files (excluding the header row).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. NewLines.append("".format(item, length=length)) ItemList.append(str(sum(map(float,itemList))))įor length,item in zip(lenghtList,itemList): The csv library contains objects and other code to read, write, and process data from and to CSV files. If you don’t, you create a so-called resource leak. As soon as you open a file with Python, you are using system resources that you need to free once you’re done. If the file was successfully opened, it returns a file object that you can use to read from and write to that file. LengthList = ] #Used for column widthsįor lineA,lineB in zip(fileALines,fileBLines): The open () function expects at least one argument: the file name. NewLines = fileALines "VIETNAMESE TOTAL" #I'm not sure how you intend on getting the column header, but you can just insert it here. What is the open() function in Python If you want to read a text file in Python, you first have to open it. In this article, I will go over the open() function, the read(), readline(), readlines(), close() methods, and the with keyword. This should do the trick: fileA = open("whatever the file name of first file is", 'r')įileB = open("whatever the file name of second file is", 'r') In Python, there are a few ways you can read a text file. I prefer to use readlines() for editing text files. ![]() ![]() Linesplit.split(" ") #3 whitespaces, since you got it that way 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.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.Backup your files before trying, the chances to delete important info is HIGH.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. Now that you are working with vars and not files you could. FYI, real 1st_files has about 100 millions of rows and twenty columns.Īlthough i agreed with iCodez and you shouldn't use txt files (maybe SQL or even json). ![]() I've tried to open 1st file with r+ mode but it did not well work. Then, write the value from 2nd_file to the corresponded VIETNAMESE column of 1st_column.įinally, calculate the values of 1st_column and write it (such as TOTAL) to the 1st_column. So, first thing is to write the header, 1) VIETNAMESE and 2) TOTAL to the header of 1st_file. Here, 2nd_file contains the values about VIETNAMESE column. Recreated 1st_file CHINESE JAPANESE KOREAN VIETNAMESE TOTAL That is, 1st file is opened as read-write mode and 2nd file is as read mode.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |