Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

python - How to row wise append two .csv files without using pandas

I want to append two .csv files with the same number/names of columns and save the ouput file. However I don't want to use pandas concat nor pandas read_csv. Is there a way to achieve this using pure python 'with open'?

question from:https://stackoverflow.com/questions/65849368/how-to-row-wise-append-two-csv-files-without-using-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something like this should work:

# read first file
with open('file1.csv', 'r') as txt:
    lines = txt.readlines()

# extend the list with the lines from the second file
with open('file2.csv', 'r') as txt:
    lines.extend(txt.readlines())

# remove 
 from the end of each line
lines = [line.strip() for line in lines]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...