
Exercise for reference:
Please concatenate this file with this one to a single text file. The content of the output file should look like in the expected output.
Answer 1:
import pandas
data1 = pandas.read_csv("http://www.pythonhow.com/data/sampledata.txt")
data2 = pandas.read_csv("sampledata_x_2.txt")
data12 = pandas.concat([data1, data2])
data12.to_csv("sampledata12.txt", index=None)
Explanation 1:
Again we are using pandas to load the data into Python. Then in line 5, we use the concat method. The method expects as input a list of dataframe objects to be concatenated. Lastly, in line 6, we export the data to a new text file.
Answer 2:
import io
import pandas
import requests
r = requests.get("http://www.pythonhow.com/data/sampledata.txt")
c = r.content
data1 = pandas.read_csv(io.StringIO(c.decode('utf-8')))
data2 = pandas.read_csv("sampledata_x_2.txt")
data12 = pandas.concat([data1, data2])
data12.to_csv("sampledata12.txt", index=None)Explanation 2:
In answer 1, we passed the file URL directly into read_csv . The read_csv method uses the urllib library internally to download the file. In case of errors with urllib you can use the more powerful library requests library as we did above.