简体   繁体   中英

How to read data from multiple text files or an csv file and create output file

I am new to Python. My requirement is:

File A (text file): 240 241 and so on

File B (text file): 250 251 and so on

Want to write a script which will take 1 record at a time from each file above and give output as:

Insert into Table A Where Num1 = 1st record from file A and Num2 = 1st record from file B Update Table A Where Num1 = 1st record from file A and Num2 = 1st record from file B

Insert into Table A Where Num1 = 2nd record from file A and Num2 = 2nd record from file B Update Table A Where Num1 = 2nd record from file A and Num2 = 2nd record from file B

Thought of making writing the static (Insert into Table A Where Num1) in file c and use it as below while writing final output file:

for open('output.txt','w') as f: f.write(data from filec + looping data from file A)

Can someone provide solution how can I get outfile data?

if the file is a text file, then you need the open two files and loop on their lines (I don't recommend using with open ("file.txt") as t when dealing with multiple files at once, so if the records are on separate lines you just need:

file1 = open("file1.txt")
file2 = open("file2.txt")

for lineOf1, lineOf2 in zip(file1, file2):

and here lineOf1 and lineOf2 are the records you need

and if the records are not on separate lines just add: lineOf1.split("the character separates the records on each line") and you will have a list of records so you can loop on it again

but if you are dealing with csv I recommend having a look at pandas library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM