Question 1: The file phone.txt stores the lines in the format code:number
import pandas as pd
import sqlite3
con = sqlite3.connect('database.db')
data = pd.read_csv('phone.txt', sep='\t', header=None)
data.to_sql('post_table', con, if_exists='replace', index=False)
I want to load all the data from the phone.txt file into the database.db database. But I have everything loaded in one column. And I need to load in two columns:
- code
- number
How to do it?
Question 2: after downloading the information to the database, how can I find the number by code? For example, if I want to find out what number code = 7 (answer: 9062621390).
Question 1
In your example pandas is not able to distinguish between the
code
and thenumber
since your file is:
-separated. When reading your file you need to change the separator to:
and also specify columns since yourcsv
doesn’t seem to have a header like soQuestion 2
After putting your data to the database you can query it as follows
where
con
is your sqlite connection.