0

I can create files with C#, with accentuated letters in their name and content:

string itemtoorder = itemorderitemlist.SelectedItem.ToString();

int amounttoorder = int.Parse(itemorderamount.Text);
if (unitselect.Text == "gramm")
{
   amounttoorder /= 1000;
}
DateTime noww = DateTime.Now;
int result = DateTime.Compare(dateTimePicker2.Value, noww);
if (result <= 0)
{
   notifyIcon1.BalloonTipText = "Nem adhatsz fel a múltba rendelést!";
   notifyIcon1.Icon = new Icon("Error.ico");
}
else
{
   string today = DateTime.Today.ToString().Replace(" ", "").Replace("0:00:00", "");
   string selecteddateasstring = dateTimePicker2.Value.Date.ToString().Replace(" ", "").Replace("0:00:00", "");
   string[] writethingie = { itemtoorder, amounttoorder.ToString(), unitselect.Text, today, 
   selecteddateasstring, "M" };
   string path = "data\\itemorders\\" + selecteddateasstring + "_" + itemtoorder + ".txt";
   if (File.Exists(path))
   {
      DialogResult dialogResult = MessageBox.Show("Már van ugyanilyen rendelés ugyanerre a napra.", "Hozzáadjam a rendelt mennyiséget?", MessageBoxButtons.YesNo);
      if (dialogResult == DialogResult.Yes)
      {
         string[] dataaaa = File.ReadAllLines(path);
         dataaaa[1] = (int.Parse(dataaaa[1]) + amounttoorder).ToString();
         File.WriteAllLines(path, dataaaa);
         notifyIcon1.BalloonTipText = "A rendelés sikeresen ki lett bővítve!";
         }
      else if (dialogResult == DialogResult.No)
      {
         notifyIcon1.BalloonTipText = "Rendelés elvetve!";
      }
   }
   else
   {
      File.WriteAllLines(path, writethingie);
      notifyIcon1.Icon = new Icon("order.ico");
      notifyIcon1.BalloonTipText = "Sikeres rendelésfelvétel!";
   }
}

I can read the file in C#, that I created. It all works. The problem is that if I create test data with a python script:

from datetime import datetime
from random import randrange
from datetime import timedelta
import string

def randomitem():
    itemlist = [
    "Ananász kocka (fagyasztott)",
    "Barna rizsliszt",
    "Barnarizs - Nagykun (szárazon)",
    "Csicseriborsó (szárazon)",
    "Csirkecomb filé (Bőr nélkül)",
    "Hámozott paradicsom konzerv - Petti",
    "Kukorica (morzsolt, fagyasztott)",
    "Marhacomb (fagyasztott)",
    "Pritamin paprika (fagyasztott)",
    "Sárgarépa csík (fagyasztott)",
    "Sárgarépa karika (fagyasztott)",
    "Sűrített paradicsom - Aranyfácán",
    "Vörösbab (szárazon)",
    "Vöröshagyma kocka (fagyasztott)",
    "Zeller (fagyasztott, csíkozott)",
    ]
    return(itemlist[randrange(len(itemlist))])
def randomitem2():
    
    itemlist = [
    "Ananász SŰRÍTMÉNY",
    "Citrom SŰRÍTMÉNY",
    "Kókuszolaj - Barco",
    "Kókusztejszín  - RealThai",
    ]
    return(itemlist[randrange(len(itemlist))])

def random_date(start, end):
    delta = end - start
    int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
    random_second = randrange(int_delta)
    return start + timedelta(seconds=random_second)

def randomamount():
    return randrange(10,70)

for i in range(200):
    d1 = datetime.strptime('2022.1.1.', '%Y.%m.%d.')
    d2 = datetime.strptime('2023.12.31.', '%Y.%m.%d.')
    thida = str(random_date(d1, d2))

    d3 = datetime.strptime('2021.4.1.', '%Y.%m.%d.')
    d4 = datetime.strptime('2023.6.28.', '%Y.%m.%d.')
    thirec = random_date(d3, d4)

    if(i>160):
        thisitem = randomitem2()
    else:
        thisitem = randomitem()

    file = open(thida.replace("-",".").split()[0] + "._" + thisitem + ".txt" , "w")
    file.write(thisitem + "\n")
    file.write(str(randomamount()) + "\n") 
    if(i>160):
        file.write("liter" + "\n") 
    else:
        file.write("kilogramm" + "\n") 
    
    file.write(str(thirec + timedelta(days=4)).replace("-",".").split()[0] + "\n") 
    file.write(str(thirec).replace("-",".").replace(" ",".") + "\n") 
    file.write(thida.replace("-",".").replace(" ",".") + "\n") 

Then the C# script can't read the accentuated letters. Here's the reader:

string[] files = Directory.GetFiles("data/warehouse/");
string[] recipé = File.ReadAllLines("data/recipes/" + allines[0] + ".txt");
List<string> goodpaths = new List<string>();
List<int> goodams = new List<int>();
for (int i = 0; i < recipé.Length - 2; i += 2)
   {
      for (int j = 0; j < files.Length - 1; j++)
      {
         string cuf = Regex.Split(files[j], "._")[1];
         cuf = cuf.Replace(".txt", "");
         //Debug.WriteLine(recipé[i]);
         //Debug.WriteLine(cuf);
         if (recipé[i] == cuf)
         {
            //Debug.WriteLine("\n--==## Match ##==--\n");
            goodpaths.Add(files[j]);
            goodams.Add(int.Parse(recipé[i + 1]));
            //Debug.WriteLine(files[j]);
            }
         }
         goodpaths.Add("BREAK");
         }
         List<int> amos = new List<int>();
         List<string> exps = new List<string>();
         List<List<string>> UOT = new List<List<string>>();
         string curitttttttt = "";
         int counter = 0;
         for (int i = 0; i < goodpaths.Count - 1; i++)
         {
            if (goodpaths[i] != "BREAK")
            {
               string[] thisisthecurrentfile = File.ReadAllLines(goodpaths[i]);
               curitttttttt = thisisthecurrentfile[0];
               //Debug.WriteLine(File.ReadAllLines(goodpaths[i])[0]);
               amos.Add(int.Parse(thisisthecurrentfile[1]));
               exps.Add(thisisthecurrentfile[5]);
            }
            else
            {
               int[] ams = amos.ToArray();
               string[] exs = exps.ToArray();
               int ned = goodams[counter];
               int amo = int.Parse(allines[1]);
               List<string>[] output = DATASET(ams, exs, ned, amo);
               //Alapanyag neve
               List<string> itnm = new List<string>();
               itnm.Add(curitttttttt);
               UOT.Add(itnm);
               //Részletek
               dataGridView1.ColumnCount = 1;
               dataGridView1.RowCount = UOT.Count;
               dataGridView1[0, counter].Value = UOT[counter][0];
               counter++;
               amos = new List<int>();
               exps = new List<string>();
                }

            }

So to sum the problem: I want to create test data with Python for a C# Windows Forms program, but the C# program can read only the files well if it creates and writes them. When the files are created by the Python script, the accentuated letters are displayed as question marks.

Gergő Szabó
  • 57
  • 1
  • 6
  • 2
    Looks like a encoding issue. Try to explicit set the encoding, when you write and when you read. – Magnetron Jun 30 '21 at 19:08
  • Sorry, I'm not sure what explicit encoding exactly means – Gergő Szabó Jun 30 '21 at 19:10
  • Well, I'm not very familiar with python, but I guess is [something like this](https://stackoverflow.com/a/6048203/5762332). In C# there is usually [an overload that takes encoding as an argument](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-5.0#System_IO_File_ReadAllLines_System_String_System_Text_Encoding_). – Magnetron Jun 30 '21 at 19:55
  • @GergőSzabó please provide the error you got. One [encoding](https://en.wikipedia.org/wiki/Encoding) is a set of rules on how "human-level information" (accentuated letters, mathematical symbols, emojis, ...) gets converted into "machine-level information" (bytes, endianness, ...) and back. For historical and performance reasons, there exist many different encodings. When two programs have to communicate (read a file that was written by the other), they have to use the same encoding, otherwise what is read is not what was written. Add explicit encoding instructions when writting and reading. – Lenormju Jul 01 '21 at 07:58

0 Answers0