嘿伙计们我正在使用python我只需要知道我的代码中我做错了什么这是代码的剪辑,这是我猜的错误:
Uni2Rows = []
R2Rows = 0
for rows in filter:
R2Rows += 1
if R2Rows > 3 and R2Rows < 34 and rows[0] is not '':
Uni2Rows.append(rows)
GradUnderGrad = {'No.': ('Company', 'Grad', 'UnderGrad')}
for i, out2put in enumerate(Uni2Rows):
total = 0
for roww in Uni2Rows [11:12]:
if roww[out2put] is not '':
total += 1
GradUnderGrad.append(['No', twoColumns, UniRows[11], UniRows[12]])
print(GradUnderGrad)
这是我整个程序的代码,我也说了我希望我的预期答案看起来如何,但我实际得到的是什么
####please dont copy straight up its like stealing an essay some things you cant really change theres only so many ways to do it
##Code by Angel M Gonzalez
import csv
with open('scratch.csv', 'r', errors='replace') as progfile:
progfile.close()
a = open("scratch.csv")
filter = csv.reader(a)
# Step 2: Printing
columns = { 0: 'Company', 1: 'Booth', 2: 'Full-Time', 3: 'Full-Time Visa Sponsor', 4: 'Part-Time', 5: 'Internship', 6: 'Freshman', 7: 'Sophomore', 8: 'Junior', 9: 'Senior', 10: 'Post-Bacs', 11: 'MS', 12: 'PhD', 13: 'Alumni', }
for i in columns:
print(i, columns[i])
# Step 3: Filtering and Formatting
UniRows = []
RRows = 0
for rows in filter:
RRows += 1
if RRows > 3 and RRows < 34 and rows[0] is not '':
UniRows.append(rows)
#Step 4 Expected output
employersSummary = {'No.': ('Column', 'Sum')}
for output in range(14):
total = 0
for rrows in UniRows:
if rrows[output] is not '':
total += 1
employersSummary[output] = (columns[output], total)
print(employersSummary)
#Step 4 continued
twoColumns = {
0: 'AIG',
1: 'Baylor College of Medicine',
2: 'CGG',
3: 'Citi',
4: 'ExxonMobil',
5: 'Flow-Cal Inc.',
6: 'Global Shop Solutions',
7: 'Harris County CTS',
8: 'HCSS',
9: 'Hitachi Consulting',
10:'HP Inc.',
11: 'INT Inc.',
12: 'JPMorgan Chase & Co',
13: 'Leidos',
14: 'McKesson',
15: 'MRE Consulting Ltd.',
16: 'NetIQ',
17: 'PROS',
18: 'San Jacinto College',
19: 'SAS',
20: 'Smartbridge',
21: 'Sogeti USA',
22: 'Southwest Research Institute',
23: 'The Reynolds and Reynolds Company ',
24: 'UH Enterprise Systems',
25: 'U.S. Marine Corps',
26: 'ValuD Consuting LLC',
27: 'Wipro',}
for i in twoColumns:
print(i, twoColumns[i])
#Step 4 continued
Uni2Rows = []
R2Rows = 0
for rows in filter:
R2Rows += 1
if R2Rows > 3 and R2Rows < 34 and rows[0] is not '':
Uni2Rows.append(rows)
GradUnderGrad = {'No.': ('Company', 'Grad', 'UnderGrad')}
for i, out2put in enumerate(Uni2Rows):
total = 0
for roww in Uni2Rows [11:12]:
if roww[out2put] is not '':
total += 1
GradUnderGrad.append(['No', twoColumns, UniRows[11], UniRows[12]])
print(GradUnderGrad)
这是我想要的最后一步结果:
No Company Grad UnderGrad
0 AIG, 1, 1
1 Baylor College of Medicine, 0, 0
2 CGG, 1, 0
3 Citi, 0, 1
4 ExxonMobil, 0, 1
5 Flow-Cal Inc., 0, 1
6 Global Shop Solutions, 0, 1
7 Harris County CTS, 1, 1
8 HCSS, 1, 1
9 Hitachi Consulting, 1, 1
10 HP Inc., 1, 1
11 INT Inc., 1, 1
12 JPMorgan Chase & Co, 0, 1
13 Leidos, 1, 1
14 McKesson, 0, 1
15 MRE Consulting Ltd., 1, 1
16 NetIQ, 0, 1
17 PROS, 1, 1
18 San Jacinto College
19 SAS, 1, 1
20 Smartbridge, 1, 1
21 Sogeti USA, 1, 1
22 Southwest Research Institute, 1, 1
23 The Reynolds and Reynolds Company 0 1 24 UH Enterprise Systems, 1, 1
25 U.S. Marine Corps, 1, 1,
26 ValuD Consuting LLC, 0, 1
27 Wipro, 0
Total 17 26
但这就是我不断得到的东西而且令人讨厌:
[No Company Grad UnderGrad]
我的问题是本科的最后一步我使用的是csv文件。有谁知道我做错了什么?我没有正确合并列我是否需要一个不同的代码,如果有人帮助我,请做我需要帮助。
这是我正在使用的CSV文件的链接:https://github.com/ag0715/Assign3ddhudhudhuhsashdui3hrfuwhf94839fuh38u4fhui/blob/master/CSV%20FILE
答案 0 :(得分:0)
为什么使用如此复杂的方法来读取某些行并找到所需的字符串。
以下是您可以使用的示例代码,以实现您的目标:
import csv
import re
import os
repeatCheck = []
dataTOWorkWith = []
print(os.getcwd())
with open('task2.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=' ')
idx = 0
for line in list(csv_reader)[3:]:
rowdata = ",".join(i for i in line)
if "BOOTH" in rowdata :
break
if (not rowdata.startswith("," or ".")) and (rowdata not in repeatCheck) and (rowdata != ""):
dataTOWorkWith.append( str(idx) + "," + (re.sub('[^a-zA-Z0-9\n\.,]', ' ', rowdata )))
idx+=1
repeatCheck.append(rowdata)
with open('task4.csv', 'w') as csv_file:
banner = "No Company Grad UnderGrad"
csv_file.write(",".join(x for x in banner.split(" "))+"\n")
print(banner)
for data in dataTOWorkWith :
rowToPrint = " ".join(data.split(",")[0:2])+" "
rowdataToAppend = ",".join(data.split(",")[0:2])+","
underGrand = "".join(data.split(",")[7:10]).rstrip()
Grand = "".join(data.split(",")[11:12]).rstrip()
if Grand != "" :
rowToPrint+=str(1)+" "
rowdataToAppend+=str(1)+","
else:
rowToPrint += str(0) + " "
rowdataToAppend += str(0) + ","
if underGrand != "" :
rowdataToAppend += str(1) + ","
rowToPrint+=str(1)
else:
rowdataToAppend += str(0) + ","
rowToPrint += str(0)
grandUgrad = Grand + "," + underGrand
print(rowToPrint)
csv_file.write(rowdataToAppend+"\n")
<强>输出强>
No Company Grad UnderGrad
0 AIG 1 1
1 Baylor College of Medicine 0 0
2 CGG 1 0
3 Citi 0 1
4 ExxonMobil 0 1
5 Flow Cal Inc. 0 1
6 Global Shop Solutions 0 1
7 Harris County CTS 1 1
8 HCSS 1 1
9 Hitachi Consulting 1 1
10 HP Inc. 1 1
11 INT Inc. 1 1
12 JPMorgan Chase Co 0 1
13 Leidos 1 1
14 McKesson 0 1
15 MRE Consulting Ltd. 1 1
16 NetIQ 0 1
17 PROS 1 1
18 San Jacinto College 1 1
19 SAS 1 1
20 Smartbridge 1 1
21 Sogeti USA 1 1
22 Southwest Research Institute 1 1
23 The Reynolds and Reynolds Company 0 1
24 UH Enterprise Systems 1 1
25 U.S. Marine Corps 1 1
26 ValuD Consuting LLC 0 1
27 Wipro 0 1