ソースを参照

merge csv rows

bscheibel 5 年 前
コミット
7ec35e3902
共有6 個のファイルを変更した52 個の追加4 個の削除を含む
  1. 1 1
      .idea/dxf_reader.iml
  2. 1 1
      .idea/misc.xml
  3. 1 1
      dxf_line_reader.py
  4. 43 0
      merge_lines.py
  5. 4 0
      merge_pandas.py
  6. 2 1
      test2.py

+ 1 - 1
.idea/dxf_reader.iml

@@ -4,7 +4,7 @@
     <content url="file://$MODULE_DIR$">
       <excludeFolder url="file://$MODULE_DIR$/venv" />
     </content>
-    <orderEntry type="jdk" jdkName="Python 3.7" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.7 (dxf_reader)" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
   <component name="TestRunnerService">

+ 1 - 1
.idea/misc.xml

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (dxf_reader)" project-jdk-type="Python SDK" />
 </project>

+ 1 - 1
dxf_line_reader.py

@@ -27,7 +27,7 @@ with open(filepath,'r', errors="replace") as fp:
     #buffer = []  # and start a new one
     #buffer.append(line)
     cnt += 1
-f.close()
+#f.close()
 
 #printpoint(buffer)        # buffer left over from last pass through loop
 

+ 43 - 0
merge_lines.py

@@ -0,0 +1,43 @@
+import csv
+
+## open CSV file and rea it
+myfile  = open('text.csv', "r")
+reader = csv.reader(myfile, delimiter=";")
+## create an empty dictionary
+mydictionary = {}
+
+rownum = 0
+
+for row in reader:
+    ## check if it is the header
+    if rownum == 0:
+        pass
+    else:
+        ## split the line of CSV in elements..Use the name for the key in dictionary and the other two in a list
+        #line = row.split(";")
+        #print(row)
+        text = row[0]
+        #print(text)
+        x = row[1]
+        y = row[2]
+
+        if x in mydictionary:
+            mydictionary[text][1] += text
+            print(mydictionary[text][1] )
+        else:
+            mydictionary[text] = [x,y]
+
+    rownum += 1
+
+myfile.close()
+
+## create a new list of lists with the data from the dictionary
+newcsvfile = ["text","x","y"]
+
+for i in mydictionary:
+    newcsvfile.append(mydictionary[i])
+
+## write the new list of lists in a new CSV file
+with open("output.csv", "wb") as f:
+    writer = csv.writer(f)
+    writer.writerows(newcsvfile)

+ 4 - 0
merge_pandas.py

@@ -0,0 +1,4 @@
+import pandas
+df = pandas.read_csv('text.csv', header = 0)
+df['Total'] = df.groupby(['Name', 'Age'])['Salary'].transform('sum')
+df.drop_duplicates(take_last=True)

+ 2 - 1
test2.py

@@ -1,4 +1,5 @@
 import csv
+import math
 
 def printsection(b):
     #print(b)
@@ -10,7 +11,7 @@ def printsection(b):
                 #print('{},{}'.format(obj['10'], obj['20']))
                 #print("\n")
 
-                row = [values,obj['10'],obj['20']]
+                row = [values,math.floor(float(obj['10'])),math.floor(float(obj['20']))]
                 with open('text.csv', 'a') as csvFile:
                     writer = csv.writer(csvFile, delimiter =';')
                     if row[0] != '':