Posts: 16
Threads: 4
Joined: Oct 2018
Oct-11-2018, 05:26 PM
(This post was last modified: Oct-11-2018, 06:18 PM by SpencerH.)
I have almost completed my program, but I am not sure what to do next. Please stick with me as I try to explain.
Currently, I get a list from email and it is formatted with information that I want to print. However, everything is out of order.
I formate what I want to print using print statements and output to text files. But I can only do this for lists of 1 and only for the first item because I'm unsure of how to make my loop work right.
The list comes to me as a list of lists.
How can I iterate through my list building the correct results?
When I pull the email and process it I get:
['\ufeff73450', 'New Hope Electric Company of Washington, Inc.', 'CI-120 - West Port Hall Student Commons - AC-Aiphone-Intrusion-CCTV', 'Location', 'Part#', 'Description', 'Curr Qty', 'M2C2', '6702UE 888U1000', 'Cable - 4 Conductor-SSA-Plenum-Box-Unshielded-22AWG/4-Stranded-Natural', '1000', '']
['\ufeff76805', 'JM Wentworth, Inc.', 'JMW - Westport - Buildout', 'Location', 'Part#', 'Description', 'Curr Qty', 'L1B2', '98756-U48', 'Patch Panel-Cat6-48 Port-2U-Loaded-Universal-Black', '1', 'L1B3', '65756-U24', 'Patch Panel-Cat6-24 Port-1U-Loaded-Universal-Black', '1', 'M3B4', '95-050-41-X', 'Fiber Connector-MM-Unicam-Ceramic-SC-50/125-OM3/OM4-High performance-Aqua', '24', 'M3B7', 'RIC-F-SC12-01', 'Fiber Bulkhead-SM/MM-6 Duplex-12 Adapters-SC-Black', '2', 'M3B9', 'RIC-F-BLNK-01', 'Fiber Bulkhead - Blank-Black', '4', 'M3C9', '852-L42-009', 'Fiber Patch Cord-MM-LC-SC-Duplex-50/125-Riser/CMR-3 M-Value Series-Aqua', '2', 'M2C88', '000-050', '"Fiber Ground- Connector-MC-.5"" NWT Metallic"', '2', 'M4C7', '566-000-005', 'Copper Patch Cord-Cat6-RJ45-RJ45-5 Ft-No boot-PVC-Stranded-Value Series-Blue', '37', 'M4C6', '2601-00000-015', 'Copper Patch Cord-Cat6A-RJ45-RJ45-15 Ft-Snagless-PVC-Shielded-Stranded-Gray', '2', ''] Currently to get things to print how I want I use this:
print("_" * 45, file=open("output.txt", "a"))
print("| Job Number: " + "73543",
" " * (25 - len(attachment_all[0][7])) + "| ",
file=open("output.txt", "a"))
print("| Job Name: " + attachment_all[0][1],
" " * (18 - len(attachment_all[0][7])) + "| ",
file=open("output.txt", "a"))
print("| Job Location: " + attachment_all[0][2],
" " * (17 - len(attachment_all[0][7])) + "| ",
file=open("output.txt", "a"))
print("*" * 45, file=open("output.txt", "a"))
print("| Loc | Part #", " " * (17) + "| Qty |", file=open("output.txt", "a"))
print("| " + attachment_all[0][7],
" " * (4 - len(attachment_all[0][7])) + "| "
+ attachment_all[0][8],
" " * (23 - len(attachment_all[0][8])) + "| "
+ attachment_all[0][10],
" " * (10 - len(attachment_all[0][10])) + "|",
file=open("output.txt", "a"))
print("| Description: " + attachment_all[0][9], file=open("output.txt", "a")) *Notice I put the job number if I don't I get an error, but I am worried about how I can iterate through my list to get things to print the right way. The heading will always be the same but when a list has more than 1 part it will not repeat the headline and just add a new line for the items.
I'm reading through some other posts and getting ideas.
I know for instance I want to grab items 0-6 inside any new list. This will be my header which is 1-12 in my print section.
Next, I want to grab any set of 4 items, 7-11, and make that my next line. Follow this line with any 4 more items until no items are left.
So far I don't know how to do this part.
Posts: 11,871
Threads: 474
Joined: Sep 2016
In this case, It would be easier on our end if you show what you want the output to look like,
in addition to the code.
I am sure that there is a much simpler solution.
Posts: 5,150
Threads: 396
Joined: Sep 2016
Oct-12-2018, 02:03 AM
(This post was last modified: Oct-12-2018, 02:03 AM by metulburr.)
i would just organize it, then work on the way it is presented, then write it to a file....not all at once.
kinda like this
import pprint
pp = pprint.PrettyPrinter()
soup = [
['\ufeff73450', 'New Hope Electric Company of Washington, Inc.', 'CI-120 - West Port Hall Student Commons - AC-Aiphone-Intrusion-CCTV', 'Location', 'Part#', 'Description', 'Curr Qty', 'M2C2', '6702UE 888U1000', 'Cable - 4 Conductor-SSA-Plenum-Box-Unshielded-22AWG/4-Stranded-Natural', '1000', ''],
['\ufeff76805', 'JM Wentworth, Inc.', 'JMW - Westport - Buildout', 'Location', 'Part#', 'Description', 'Curr Qty', 'L1B2', '98756-U48', 'Patch Panel-Cat6-48 Port-2U-Loaded-Universal-Black', '1', 'L1B3', '65756-U24', 'Patch Panel-Cat6-24 Port-1U-Loaded-Universal-Black', '1', 'M3B4', '95-050-41-X', 'Fiber Connector-MM-Unicam-Ceramic-SC-50/125-OM3/OM4-High performance-Aqua', '24', 'M3B7', 'RIC-F-SC12-01', 'Fiber Bulkhead-SM/MM-6 Duplex-12 Adapters-SC-Black', '2', 'M3B9', 'RIC-F-BLNK-01', 'Fiber Bulkhead - Blank-Black', '4', 'M3C9', '852-L42-009', 'Fiber Patch Cord-MM-LC-SC-Duplex-50/125-Riser/CMR-3 M-Value Series-Aqua', '2', 'M2C88', '000-050', '"Fiber Ground- Connector-MC-.5"" NWT Metallic"', '2', 'M4C7', '566-000-005', 'Copper Patch Cord-Cat6-RJ45-RJ45-5 Ft-No boot-PVC-Stranded-Value Series-Blue', '37', 'M4C6', '2601-00000-015', 'Copper Patch Cord-Cat6A-RJ45-RJ45-15 Ft-Snagless-PVC-Shielded-Stranded-Gray', '2', '']
]
data = {}
#assign key names to known indexes
for lst in soup:
key = lst[1] #set the second element (the name) of each list as the dictionary key
data.update({key:{}})
data[key].update({'num':lst[7]})
data[key].update({'loc':lst[2]})
data[key].update({'part':lst[4]})
data[key].update({'desc':lst[9]})
pp.pprint(data) # example of data organized
#assign output presentation
for k in data:
d = f'''
| Job Number: {' ': <25} {data[k]['num']} |
| Job Name: {' ': <25} {k} |
| Job Location: {' ': <25} {data[k]['loc']} |
'''
print(d) #now write d to a file instead of printing it
its python3.6+ because i used the f-string otherwise you would do
d = '''
| Job Number: {SPACE: <25} {NUM} |
| Job Name: {SPACE: <25} {KEY} |
| Job Location: {SPACE: <25} {LOC} |
'''.format(
SPACE=' ',
NUM=data[k]['num'],
KEY=k,
LOC=data[k]['loc']
)
Recommended Tutorials:
Posts: 16
Threads: 4
Joined: Oct 2018
(Oct-12-2018, 02:03 AM)metulburr Wrote: i would just organize it, then work on the way it is presented, then write it to a file....not all at once.
Your example gets me pretty close. But I'm still not seeing how to process when the item contains more than one "part". Indexes 7-10 will always be a part but so will every 4 positions after that. 11-14, 15-18, etc but only if they are there of course.
I had started down a path of trying to feed the data into JSON. I think your example helps me see a way to do that but I still have the same problem
Posts: 16
Threads: 4
Joined: Oct 2018
Oct-12-2018, 02:04 PM
(This post was last modified: Oct-12-2018, 02:06 PM by SpencerH.)
Maybe this will explain what I want to do even though this code is none functioning:
list = ['1', '2', '3', '4', '5', '6']
for i in list:
while i [3] or more in i:
composite_list = [list[x:x+4] for x in rangee90, len(list),5]
Posts: 5,150
Threads: 396
Joined: Sep 2016
Oct-12-2018, 04:08 PM
(This post was last modified: Oct-12-2018, 04:08 PM by metulburr.)
(Oct-12-2018, 11:52 AM)SpencerH Wrote: But I'm still not seeing how to process when the item contains more than one "part". Indexes 7-10 will always be a part but so will every 4 positions after that. 11-14, 15-18, etc but only if they are there of course. Pop off the first 10 indexes, as they will always be there right? Then pop off the next 4 indexes, and repeat until you get an IndexError as it will either be empty or out of range when the list goes below 4 indexes. As you are popping off the sections add them into your dictionary.
Recommended Tutorials:
Posts: 16
Threads: 4
Joined: Oct 2018
I did that in a way:
for item in happy_days:
the_real_deal = str(base64.b64decode(item).decode())
# print(the_real_deal)
testing_the_split = re.split('[\t\r\n]+', the_real_deal)
#print(testing_the_split)
# list_of_lists = [list(elem) for elem in the_real_deal]
# print(list_of_lists)
header_list = testing_the_split[:3]
parts_list = testing_the_split[7:]
print("_" * 45, file=open("output.txt", "a"))
print("| Job Number: " + header_list[0],
" " * (25 - len(header_list[0])) + "| ",
file=open("output.txt", "a"))
print("| Job Name: " + header_list[1],
" " * (18 - len(header_list[1])) + "| ",
file=open("output.txt", "a"))
print("| Job Location: " + header_list[2],
" " * (17 - len(attachment_all[0][2])) + "| ",
file=open("output.txt", "a"))
print("*" * 45, file=open("output.txt", "a"))
print("| Loc | Part #", " " * (17) + "| Qty |", file=open("output.txt", "a"))
print("| " + parts_list[0],
" " * (4 - len(parts_list[0])) + "| "
+ parts_list[1],
" " * (23 - len(parts_list[1])) + "| "
+ parts_list[3],
" " * (10 - len(parts_list[3])) + "|",
file=open("output.txt", "a"))
print("| Description: " + parts_list[2], file=open("output.txt", "a")) I am still not processing beyond 1 part on my list but I have a new problem.
When I run this I get:
Error: Traceback (most recent call last):
File "<input>", line 13, in <module>
File "C:\Users\Kingdel\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufeff' in position 14: character maps to <undefined>
This is because when I return the first object in the list it looks like ['\ufeff73543', ... I'm stumped for a simple solution.
Posts: 5,150
Threads: 396
Joined: Sep 2016
thats an encoding issue because you are writing it to a file. Thats why i said separate what you are doing into steps. Writing to a file is the last step. You still havent figured out how to organize the data yet.
But read this for your issue
https://stackoverflow.com/questions/1463...-undefined
Recommended Tutorials:
Posts: 16
Threads: 4
Joined: Oct 2018
Oct-12-2018, 06:27 PM
(This post was last modified: Oct-12-2018, 06:27 PM by SpencerH.)
(Oct-12-2018, 04:18 PM)metulburr Wrote: thats an encoding issue because you are writing it to a file. Thats why i said separate what you are doing into steps. Writing to a file is the last step. You still havent figured out how to organize the data yet.
But read this for your issue
https://stackoverflow.com/questions/1463...-undefined
I was able to find when I write the file I just put in encoding="utf-8"
So I got the encode/wirte to file funciton fixed.
for item in happy_days:
the_real_deal = str(base64.b64decode(item).decode("utf8"))
# print(the_real_deal)
testing_the_split = re.split('[\t\r\n]+', the_real_deal)
# print(testing_the_split)
# list_of_lists = [list(elem) for elem in the_real_deal]
# print(list_of_lists)
header_list = testing_the_split[:3]
parts_list = testing_the_split[7:]
print("_" * 45, file=open("output.txt", "a"))
print("| Job Number: " + header_list[0],
" " * (25 - len(header_list[0])) + "| ",
file=open("output.txt", "a", encoding="utf-8"))
print("| Job Name: " + header_list[1],
" " * (18 - len(header_list[1])) + "| ",
file=open("output.txt", "a"))
print("| Job Location: " + header_list[2],
" " * (17 - len(attachment_all[2])) + "| ",
file=open("output.txt", "a"))
print("*" * 45, file=open("output.txt", "a"))
print("| Loc | Part #", " " * (17) + "| Qty |", file=open("output.txt", "a"))
print("| " + parts_list[0],
" " * (4 - len(parts_list[0])) + "| "
+ parts_list[1],
" " * (23 - len(parts_list[1])) + "| "
+ parts_list[3],
" " * (10 - len(parts_list[3])) + "|",
file=open("output.txt", "a"))
print("| Description: " + parts_list[2], file=open("output.txt", "a"))
print("there was " + header_list[0])
#os.system("C:/Users/Kingdel/PycharmProjects/ScannerMailHandler/printanddelete.exe")
time.sleep(5) The pprint gave me the ideas to split the string. Now I am stuck wondering how I can format correctly for the second part.
In the code above [0,1,2,3,4,5,6,7,8,9,10] will get split into
header_list = [0,1,2]
parts_list = [7,8,9,10]
And will output this:
_____________________________________________
| Job Number: 0 |
| Job Name: 1 |
| Job Location: 2 |
*********************************************
| Loc | Part # | Qty |
| 7 | 8 | 9 |
| Description: 10 However if my list is [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
[11:] will be dropped but I need to process it to the next line like this:
_____________________________________________
| Job Number: 0 |
| Job Name: 1 |
| Job Location: 2 |
*********************************************
| Loc | Part # | Qty |
| 7 | 8 | 9 |
| Description: 10
|11 | 12 | 13 |
| Description: 14
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Oct-12-2018, 06:27 PM)SpencerH Wrote: file=open("output.txt", "a") Please, just open the file once. Seeing that on every line is hurting my brain.
Something like with open("output.txt", "a") as out:
print("something", file=out)
print("something else", file=out)
|