Python : saving variables from a text file created

RobMBSos

I have been trying to split lines from a file and save that split data into variables without success.

My code is now like this:

cur = db.cursor() 
query = """INSERT INTO xxxx (var1, var2) VALUES (%s, %s)"""
with open('data.txt') as f:
    da = f.readlines()
    for line in da:
        values = (v1,v2)
        cur.execute(query,values)

But it doesn't work as I want.

My request is if any of you can help me to get from data.txt the values needed and saved to variables to be able to send them as a query to the database.

data.txt has now this:

0013a200419e323b <=>€#356894040#XBEE#0#STR:XBee frame#BAT:1#
0013a200419e323b <=>€#356894040#XBEE#0#STR:XBee frame#BAT:1#

So var1 has to be equal to 0013a200419e323b and var2 to <=>€#356894040#XBEE#0#STR:XBee frame#BAT:1#

Padraic Cunningham

you need to split once on white space:

with open('data.txt') as f: 
    for line in f: # can iterate over f, no need for readlines
       values = line.split(None, 1) # splits on first whitespace only
       cur.execute(query,values)

You will get a list containing the two substrings you want:

In [13]: s="0013a200419e323b <=>€#356894040#XBEE#0#STR:XBee frame#BAT:1#"

In [14]: s.split(None,1)
Out[14]: ['0013a200419e323b', '<=>\xe2\x82\xac#356894040#XBEE#0#STR:XBee frame#BAT:1#']
In [15]: var1,var2 = s.split(None,1)
In [16]: var1
Out[16]: '0013a200419e323b'
In [17]: var2
Out[17]: '<=>\xe2\x82\xac#356894040#XBEE#0#STR:XBee frame#BAT:1#'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Saving list from python to text file in format

From Dev

Saving text file from code

From Dev

Saving a text file from flash

From Dev

Python - sum variables from a text file

From Dev

Python: reading in a csv file and saving columns as variables

From Dev

Python Saving long string in text file

From Dev

Saving a dictionary to a text file python 3

From Dev

Saving gif file created from screenshots to camera roll

From Dev

Create variables from text file

From Dev

File is being created but data is not saving?

From Dev

Saving ArrayList to Text File

From Dev

Saving PairedRDD as a text file

From Dev

Saving ArrayList to Text File

From Dev

Extracting data from a text file using grep and saving it in another directory

From Dev

Importing data from text file and saving the same in excel

From Dev

Saving data internally to a text file from keyboard input

From Dev

Saving text from TextView

From Dev

Saving a text from UIAlertView

From Dev

Saving text from TextView

From Dev

saving a structure of a list with the data in a text file using Python

From Dev

saving variables in formatted ASCII file

From Dev

Batch command to pull variables from a text file

From Dev

ActionScript: loading variables from a text file into _root

From Dev

Weird issue passing variables from text file

From Dev

Plucking variables from a text file in PHP

From Dev

Powershell script getting variables from a text file

From Dev

Datepicker reads variables from text file

From Dev

Powershell reading multiple variables from text file

From Dev

from DataGridView saving to text file issue when saving date values on vb.net