HI all
I'm reading data from a CSV (text file) with a "| " as the denominator. Every is working as planned but I'm having a problem with the last 3 lines of code. In the text Document the 'RB' (Body) has more than one entry exp.
RB | INV000111 | price to be charged
RB | INV000112 | price 2 to be charged
some has more then TWO entry. Now the problem being
When Python reads the CSV it only writes the Second line to the PDF INV000112 not both lines as it is displayed.
How can I get it to write Both lines as displayed in the text file - db = csv.reader(open('C:/Users/rynod/Desktop/cpc.txt','rb'), delimiter = '|', quotechar='"')
-
for row in db:
-
for field in enumerate(row):
-
if 'H' in field:
-
#Customer Details
-
CustPAName = row[9]
-
CustPA1 = row[10]
-
CustPA2 = row[11]
-
CustPA3 = row[12]
-
CustPA4 = row[13]
-
CustPA5 = row[14]
-
CustFAName = row[18]
-
CustFA1 = row[19]
-
CustFA2 = row[20]
-
CustFA3 = row[21]
-
CustFA4 = row[22]
-
CustEMail = row[15]
-
#Sharp Details
-
SharpAddress1 = row[2]
-
SharpAddress2 = row[3]
-
SharpAddress3 = row[4]
-
SharpAddress4 = row[5]
-
SharpTel = row[6]
-
SharpFax = row[7]
-
Model = row[16]
-
Serial = row[17]
-
Account = row[25]
-
OurRef = row[27]
-
elif 'RB' in field:
-
print(row)
-
Decs = row
3 2178 bvdet 2,851
Recognized Expert Moderator Specialist
Why do you need to do a nested for loop? You should be able to do it in one loop. Why not compile a dictionary instead of assigning so many variables? It would work something like the following where data is a line in db - fields = ['SharpAddress1',
-
'SharpAddress2',
-
'SharpAddress3',
-
'SharpAddress4',
-
'SharpTel',
-
'SharpFax']
-
-
data = [0,1,2,3,4,5,6,7,8,9,10]
-
-
dd = dict(zip(fields, data))
I am not sure why only one "RB" line is printed other than it has something to do with the nested loop or your use of enumerate. Could you post a representative portion of the CSV file?
I'm trying to write a report in python where the data is read from a text file with CSV and then using report lab producing a PDF file(Invoice)
In the text file We have different sections 'H' Header 'B' Body ect. so that is why I'm using more then one loop. it is reading the text file and using row to assign it to a verable that gets coped to the PDF.
I manage to get it to read the RB section by using the following - data = []
-
for i in range(1):
-
if 'RB' in field:
-
data.append(row[3])
-
print(data[1:2])
-
cal = str(data[0:1])
-
cal2 = str(data[1:2])
But the problem is now it shows it as a list as a string with the '[]' how do I get the '[]' not to show.
['546 @ 0.0879 cents = R47.99']
['165 @ 0.0967 cents = R15.96']
And also I don't thing this is very efficient because it will break if I have more then 2 entry
And as you probably guest I'm also just started learning python.
Thanks for your help so far
dwblas 626
Recognized Expert Contributor
Your first problem is using enumerate - row = ["ZHZ", "AH", "HA"]
-
for field in enumerate(row):
-
print field, "--> H",
-
if 'H' in field:
-
print "foumd"
-
else:
-
print " NOT found" [
You don't use it so if we take it out - row = ["ZHZ", "AH", "HA"]
-
for field in row:
-
print field, "--> H",
-
if 'H' in field:
-
print "foumd"
-
else:
-
print " NOT found"
but this will find any "H" anywhere in the record. Is that what you want to do? Some test data would help.
use .join() to convert a list to a string - print("*".join(data[1:2]))
but data[1:2] == data[1] so you can just print data[1] in this specific case.
Every is working as planned but I'm having a problem with the last 3 lines of code. In the text Document the 'RB' (Body) has more than one entry exp.
RB | INV000111 | price to be charged
RB | INV000112 | price 2 to be charged
Using csv is overkill IMHO in this case. Just read the file and split each record. - """
-
row = ["ZHZ", "AH", "HA"]
-
""" commented out because "test_it" is used to simulate the file
-
with open(file_name, "r") as fp: ## replaced by test_it
-
for rec in fp: ## replaced by for rec in test_it
-
"""
-
test_it= ['ABC | INV001 | price',
-
'HB | INV000111 | price to be charged',
-
'RB | INV000112 | price 2 to be charged']
-
-
for rec in test_it:
-
data = rec.split("|")
-
print data[0], "--> HB",
-
if 'HB' == data[0].strip(): ## strip white space in file
-
print "foumd"
-
else:
-
print " NOT found"
And also I don't thing this is very efficient because it will break if I have more then 2 entry
Write each entry to the pdf file as it is found, then it doesn't matter how many there are.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mike Dole |
last post by:
I'm working on a client - server application based on the 'How to
Sockets Server and How to Sockets Client' code from the Visual Basic
..NET Resource Kit.
Since I want to be able to send 'big...
|
by: Mark Dunmill |
last post by:
I can't create a Constant/Read-only array field in managed C++ classes -
doesn't allow the keyword const pointer to const object on array fields in
managed C++ classes. e.g. Want to define a...
|
by: Ron |
last post by:
What is the VB equivalent of the READ...DATA statements in
QuickBasic?
Thanks
|
by: rossum |
last post by:
Is there any way of creating a read only reference to an array? My
class includes a private array of bytes with a Property to access it.
However, I do not want users to use the returned reference...
|
by: Cristina |
last post by:
Hello ,
How can I read an array of integers from an external file ? Each array is in one line and i don-t know the number of integers.I don`t want to use a string of chars, I need them as integers...
| |
by: Francois Stander |
last post by:
Hi, hope someone can help me. It seems imposible to read data from a server, however, I can read the validation data from the server and hold it in dataviews . datasets or data tables in my asp...
|
by: Bill Fallon |
last post by:
I have a VS2005 VB.Net windows form application deployed to a share drive.
The windows explorer security permissions for this application (.exe) file
is set for Everyone with List Folder/Read Data...
|
by: barbara_dave |
last post by:
Hi All,
I need to read data from a Excel spreadsheet, but I got the problem
when I tried the code below:
StringBuilder sbConn = new StringBuilder();...
|
by: mwenz |
last post by:
I am trying to update an Access table using OLEDB in VB.Net 2005. I can add rows but I cannot update them.
Code to instantiate the Access database and table...
Dim conn As New...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |